Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
K
key_value-server
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
amir.yosef
key_value-server
Commits
5fed48e8
Commit
5fed48e8
authored
Sep 01, 2024
by
amir.yosef
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Final
parent
49e510a1
Changes
17
Show whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
362 additions
and
22 deletions
+362
-22
CommandExecutable.java
src/command/CommandExecutable.java
+0
-19
GetCommand.java
src/command/GetCommand.java
+1
-0
GetCommandValidator.java
src/command/validation/GetCommandValidator.java
+19
-0
CachePolicyFactory.java
src/storage/CachePolicyFactory.java
+16
-0
LRUCachePolicy.java
src/storage/LRUCachePolicy.java
+2
-0
Storage.java
src/storage/Storage.java
+2
-0
RdbFileReader.java
src/util/RdbFileReader.java
+1
-3
MasterClientTest.java
test/client/MasterClientTest.java
+43
-0
ReplicaClientTest.java
test/client/ReplicaClientTest.java
+29
-0
CommandInvokerTest.java
test/command/CommandInvokerTest.java
+28
-0
FullRsyncCommandTest.java
test/command/FullRsyncCommandTest.java
+62
-0
GetCommandTest.java
test/command/GetCommandTest.java
+21
-0
InfoCommandTest.java
test/command/InfoCommandTest.java
+30
-0
SetCommandTest.java
test/command/SetCommandTest.java
+35
-0
CommandFactoryTest.java
test/factory/CommandFactoryTest.java
+21
-0
ReplicaCommandFactoryTest.java
test/factory/ReplicaCommandFactoryTest.java
+28
-0
ParseCommandTest.java
test/parser/ParseCommandTest.java
+24
-0
No files found.
src/command/CommandExecutable.java
View file @
5fed48e8
package
command
;
package
command
;
import
command.validation.Validatable
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.List
;
public
interface
CommandExecutable
<
T
>
{
public
interface
CommandExecutable
<
T
>
{
T
execute
()
throws
IOException
;
T
execute
()
throws
IOException
;
class
GetCommandValidator
implements
Validatable
{
private
static
final
GetCommandValidator
INSTANCE
=
new
GetCommandValidator
();
private
GetCommandValidator
()
{
}
public
static
GetCommandValidator
getInstance
()
{
return
INSTANCE
;
}
@Override
public
boolean
isValid
(
List
<
String
>
args
)
{
return
args
!=
null
&&
args
.
size
()
==
1
;
}
}
}
}
src/command/GetCommand.java
View file @
5fed48e8
package
command
;
package
command
;
import
command.validation.GetCommandValidator
;
import
command.validation.Validatable
;
import
command.validation.Validatable
;
import
storage.Cacheable
;
import
storage.Cacheable
;
import
storage.Storage
;
import
storage.Storage
;
...
...
src/command/validation/GetCommandValidator.java
0 → 100644
View file @
5fed48e8
package
command
.
validation
;
import
java.util.List
;
public
final
class
GetCommandValidator
implements
Validatable
{
private
static
final
GetCommandValidator
INSTANCE
=
new
GetCommandValidator
();
private
GetCommandValidator
()
{
}
public
static
GetCommandValidator
getInstance
()
{
return
INSTANCE
;
}
@Override
public
boolean
isValid
(
List
<
String
>
args
)
{
return
args
!=
null
&&
args
.
size
()
==
1
;
}
}
\ No newline at end of file
src/storage/CachePolicyFactory.java
0 → 100644
View file @
5fed48e8
package
storage
;
import
server.ServiceInfo
;
public
class
CachePolicyFactory
{
private
static
final
ServiceInfo
serviceInfo
=
ServiceInfo
.
getInstance
();
public
static
CachePolicy
<
String
,
String
>
getPolicy
(
int
capacity
)
{
if
(
serviceInfo
.
getInfo
().
get
(
"cachePolicyName"
).
equals
(
"caffeine"
))
{
return
new
CaffeineCachePolicy
<>(
capacity
);
}
else
if
(
serviceInfo
.
getInfo
().
get
(
"cachePolicyName"
).
equals
(
"lru"
))
{
return
new
LRUCachePolicy
<>(
capacity
,
800000
);
}
throw
new
IllegalArgumentException
(
"Invalid CachePolicy name"
);
}
}
src/storage/LRUCachePolicy.java
View file @
5fed48e8
package
storage
;
package
storage
;
import
util.RdbFileReader
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.atomic.AtomicInteger
;
...
...
src/storage/Storage.java
View file @
5fed48e8
package
storage
;
package
storage
;
import
util.RdbFileReader
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
...
...
src/
storage
/RdbFileReader.java
→
src/
util
/RdbFileReader.java
View file @
5fed48e8
package
storage
;
package
util
;
import
util.RdbFileInfo
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
...
...
test/client/MasterClientTest.java
0 → 100644
View file @
5fed48e8
package
client
;
import
client.master.MasterClient
;
import
org.junit.Test
;
import
java.io.*
;
import
java.net.Socket
;
import
static
org
.
mockito
.
Mockito
.*;
public
class
MasterClientTest
{
@Test
public
void
test_successful_connection_and_command_processing
()
throws
IOException
{
Socket
socket
=
mock
(
Socket
.
class
);
BufferedReader
reader
=
new
BufferedReader
(
new
StringReader
(
"COMMAND\n"
));
OutputStream
outputStream
=
mock
(
OutputStream
.
class
);
when
(
socket
.
getInputStream
()).
thenReturn
(
new
ByteArrayInputStream
(
"COMMAND\n"
.
getBytes
()));
when
(
socket
.
getOutputStream
()).
thenReturn
(
outputStream
);
MasterClient
masterClient
=
new
MasterClient
(
socket
);
masterClient
.
run
();
verify
(
outputStream
,
times
(
1
)).
write
(
any
(
byte
[].
class
));
verify
(
outputStream
,
times
(
1
)).
flush
();
}
@Test
public
void
test_handle_client_with_null_buffered_reader
()
throws
IOException
{
Socket
socket
=
mock
(
Socket
.
class
);
OutputStream
outputStream
=
mock
(
OutputStream
.
class
);
when
(
socket
.
getInputStream
()).
thenReturn
(
new
ByteArrayInputStream
(
"COMMAND\n"
.
getBytes
()));
when
(
socket
.
getOutputStream
()).
thenReturn
(
outputStream
);
MasterClient
masterClient
=
new
MasterClient
(
socket
);
masterClient
.
run
();
verify
(
outputStream
,
times
(
1
)).
write
(
any
(
byte
[].
class
));
verify
(
outputStream
,
times
(
1
)).
flush
();
}
}
\ No newline at end of file
test/client/ReplicaClientTest.java
0 → 100644
View file @
5fed48e8
package
client
;
import
client.replica.ReplicaClient
;
import
org.junit.Test
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.net.Socket
;
import
static
org
.
mockito
.
Mockito
.*;
public
class
ReplicaClientTest
{
@Test
public
void
test_readLine_returns_null
()
throws
IOException
{
Socket
mockSocket
=
mock
(
Socket
.
class
);
BufferedReader
mockReader
=
mock
(
BufferedReader
.
class
);
OutputStream
mockOutputStream
=
mock
(
OutputStream
.
class
);
when
(
mockSocket
.
getOutputStream
()).
thenReturn
(
mockOutputStream
);
when
(
mockReader
.
readLine
()).
thenReturn
(
null
);
ReplicaClient
replicaClient
=
new
ReplicaClient
(
mockReader
,
mockSocket
);
replicaClient
.
run
();
verify
(
mockReader
,
times
(
1
)).
readLine
();
verify
(
mockOutputStream
,
never
()).
write
(
any
(
byte
[].
class
));
}
}
\ No newline at end of file
test/command/CommandInvokerTest.java
0 → 100644
View file @
5fed48e8
package
command
;
import
org.junit.Test
;
import
java.io.IOException
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertThrows
;
public
class
CommandInvokerTest
{
@Test
public
void
test_command_execution_success
()
throws
IOException
{
CommandExecutable
<
byte
[]>
command
=
()
->
"test"
.
getBytes
();
byte
[]
result
=
CommandInvoker
.
invoke
(
command
);
assertArrayEquals
(
"test"
.
getBytes
(),
result
);
}
@Test
public
void
test_command_execution_throws_ioexception
()
{
CommandExecutable
<
byte
[]>
command
=
()
->
{
throw
new
IOException
(
"Test exception"
);
};
assertThrows
(
IOException
.
class
,
()
->
CommandInvoker
.
invoke
(
command
));
}
}
\ No newline at end of file
test/command/FullRsyncCommandTest.java
0 → 100644
View file @
5fed48e8
package
command
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
server.SendToReplica
;
import
server.ServiceInfo
;
import
util.RdbFileInfo
;
import
java.io.ByteArrayOutputStream
;
import
java.io.OutputStream
;
import
java.util.Map
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertArrayEquals
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
class
FullRsyncCommandTest
{
@Test
public
void
test_rdb_file_content_empty
()
throws
Exception
{
SendToReplica
mockReplicaSender
=
mock
(
SendToReplica
.
class
);
RdbFileInfo
mockRdbFileInfo
=
mock
(
RdbFileInfo
.
class
);
ServiceInfo
mockServiceInfo
=
mock
(
ServiceInfo
.
class
);
OutputStream
mockOutputStream
=
new
ByteArrayOutputStream
();
byte
[]
expectedContent
=
new
byte
[
0
];
when
(
mockRdbFileInfo
.
getContent
()).
thenReturn
(
expectedContent
);
when
(
mockServiceInfo
.
getInfo
()).
thenReturn
(
Map
.
of
(
"master_replid"
,
"8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb"
));
FullRsyncCommand
command
=
new
FullRsyncCommand
(
mockReplicaSender
,
mockOutputStream
);
byte
[]
result
=
command
.
execute
();
ByteArrayOutputStream
expectedOutput
=
new
ByteArrayOutputStream
();
expectedOutput
.
write
((
"+FULLRESYNC 8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb 0\r\n"
).
getBytes
());
expectedOutput
.
write
((
"$"
+
expectedContent
.
length
+
"\r\n"
).
getBytes
());
expectedOutput
.
write
(
expectedContent
);
assertArrayEquals
(
expectedOutput
.
toByteArray
(),
result
);
}
@Test
public
void
test_execute_fullresync_command
()
throws
Exception
{
SendToReplica
mockReplicaSender
=
mock
(
SendToReplica
.
class
);
RdbFileInfo
mockRdbFileInfo
=
mock
(
RdbFileInfo
.
class
);
ServiceInfo
mockServiceInfo
=
mock
(
ServiceInfo
.
class
);
OutputStream
mockOutputStream
=
new
ByteArrayOutputStream
();
byte
[]
expectedContent
=
"testContent"
.
getBytes
();
when
(
mockRdbFileInfo
.
getContent
()).
thenReturn
(
expectedContent
);
when
(
mockServiceInfo
.
getInfo
()).
thenReturn
(
Map
.
of
(
"master_replid"
,
"8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb"
));
FullRsyncCommand
command
=
new
FullRsyncCommand
(
mockReplicaSender
,
mockOutputStream
);
byte
[]
result
=
command
.
execute
();
ByteArrayOutputStream
expectedOutput
=
new
ByteArrayOutputStream
();
expectedOutput
.
write
((
"+FULLRESYNC 8371b4fb1155b71f4a04d3e1bc3e18c4a990aeeb 0\r\n"
).
getBytes
());
expectedOutput
.
write
((
"$"
+
expectedContent
.
length
+
"\r\n"
).
getBytes
());
expectedOutput
.
write
(
expectedContent
);
Assert
.
assertArrayEquals
(
expectedOutput
.
toByteArray
(),
result
);
}
}
test/command/GetCommandTest.java
0 → 100644
View file @
5fed48e8
package
command
;
import
org.junit.Test
;
import
util.Response
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
public
class
GetCommandTest
{
@Test
public
void
test_execute_with_empty_arguments
()
{
List
<
String
>
args
=
List
.
of
();
GetCommand
getCommand
=
new
GetCommand
(
args
);
byte
[]
expectedResponse
=
Response
.
getResponse
(
"wrong args"
);
byte
[]
actualResponse
=
getCommand
.
execute
();
assertArrayEquals
(
expectedResponse
,
actualResponse
);
}
}
\ No newline at end of file
test/command/InfoCommandTest.java
0 → 100644
View file @
5fed48e8
package
command
;
import
org.junit.Test
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.*;
public
class
InfoCommandTest
{
@Test
public
void
test_valid_command_with_correct_argument
()
{
List
<
String
>
args
=
List
.
of
(
"replication"
);
InfoCommand
infoCommand
=
new
InfoCommand
(
args
);
byte
[]
response
=
infoCommand
.
execute
();
String
expectedResponse
=
"$20\r\nrole:mastercachePolicyName:caffeine\r\n"
;
assertArrayEquals
(
expectedResponse
.
getBytes
(),
response
);
}
@Test
public
void
test_null_argument_list
()
{
List
<
String
>
args
=
null
;
InfoCommand
infoCommand
=
new
InfoCommand
(
args
);
byte
[]
response
=
infoCommand
.
execute
();
String
expectedResponse
=
"$16\r\nunsupported args\r\n"
;
assertArrayEquals
(
expectedResponse
.
getBytes
(),
response
);
}
}
\ No newline at end of file
test/command/SetCommandTest.java
0 → 100644
View file @
5fed48e8
package
command
;
import
org.junit.Test
;
import
org.mockito.Mockito
;
import
storage.Cacheable
;
import
util.Response
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
public
class
SetCommandTest
{
@Test
public
void
execute_with_valid_args_size_2_returns_ok_response
()
{
List
<
String
>
args
=
Arrays
.
asList
(
"key"
,
"value"
);
Cacheable
storageMock
=
Mockito
.
mock
(
Cacheable
.
class
);
SetCommand
setCommand
=
new
SetCommand
(
args
);
byte
[]
expectedResponse
=
"+OK\r\n"
.
getBytes
();
byte
[]
actualResponse
=
setCommand
.
execute
();
assertArrayEquals
(
expectedResponse
,
actualResponse
);
}
@Test
public
void
execute_with_null_args_returns_error_response
()
{
List
<
String
>
args
=
null
;
Cacheable
storageMock
=
Mockito
.
mock
(
Cacheable
.
class
);
SetCommand
setCommand
=
new
SetCommand
(
args
);
byte
[]
expectedResponse
=
Response
.
getResponse
(
"wrong args"
);
byte
[]
actualResponse
=
setCommand
.
execute
();
assertArrayEquals
(
expectedResponse
,
actualResponse
);
}
}
\ No newline at end of file
test/factory/CommandFactoryTest.java
0 → 100644
View file @
5fed48e8
package
factory
;
import
command.CommandExecutable
;
import
command.PingCommand
;
import
model.Command
;
import
org.junit.Test
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
public
class
CommandFactoryTest
{
@Test
public
void
test_get_command_returns_ping_command
()
{
CommandFactory
factory
=
CommandFactory
.
getInstance
();
CommandExecutable
<?>
command
=
factory
.
getCommand
(
Command
.
PING
,
List
.
of
());
assertTrue
(
command
instanceof
PingCommand
);
}
}
test/factory/ReplicaCommandFactoryTest.java
0 → 100644
View file @
5fed48e8
package
factory
;
import
command.CommandExecutable
;
import
factory.replica.ReplicaCommandFactory
;
import
model.Command
;
import
org.junit.Test
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertSame
;
public
class
ReplicaCommandFactoryTest
{
@Test
public
void
test_get_instance_returns_same_instance
()
{
ReplicaCommandFactory
instance1
=
ReplicaCommandFactory
.
getInstance
();
ReplicaCommandFactory
instance2
=
ReplicaCommandFactory
.
getInstance
();
assertSame
(
instance1
,
instance2
);
}
@Test
public
void
test_get_command_returns_null_for_unknown_command
()
{
ReplicaCommandFactory
factory
=
ReplicaCommandFactory
.
getInstance
();
CommandExecutable
<?>
commandExecutable
=
factory
.
getCommand
(
Command
.
UNKNOWN
,
List
.
of
());
assertNull
(
commandExecutable
);
}
}
\ No newline at end of file
test/parser/ParseCommandTest.java
0 → 100644
View file @
5fed48e8
package
parser
;
import
org.junit.Test
;
import
java.io.BufferedReader
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Mockito
.
mock
;
public
class
ParseCommandTest
{
@Test
public
void
test_single_command_without_asterisk
()
{
CommandParser
parser
=
new
CommandParser
();
BufferedReader
bufferedReader
=
mock
(
BufferedReader
.
class
);
String
line
=
"COMMAND"
;
List
<
String
>
result
=
parser
.
parseCommand
(
bufferedReader
,
line
);
assertEquals
(
1
,
result
.
size
());
assertEquals
(
"COMMAND"
,
result
.
get
(
0
));
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment