blob: 1a808d3e84307831c08c4fefd16bd05aa086808b [file] [log] [blame]
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +04001package chunk
2
3import "context"
4import "bytes"
5import "testing"
6
7import "pcloud/api"
8
9func TestStoreChunk(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040010 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040011 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
12 ChunkId: "foo",
13 Data: []byte("hello world")})
14 if err != nil {
15 t.Error(err)
16 }
17}
18
19func TestStoreAndReadChunk(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040020 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040021 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
22 ChunkId: "foo",
23 Data: []byte("hello world")})
24 if err != nil {
25 t.Error(err)
26 }
27 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
28 ChunkId: "foo"})
29 if err != nil {
30 t.Error(err)
31 }
32 if bytes.Compare(resp.Data, []byte("hello world")) != 0 {
33 t.Errorf("Expected: %s\nGot: %s\n", "hello world", resp.Data)
34 }
35}
36
37func TestReadWithOffsets(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040038 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040039 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
40 ChunkId: "foo",
41 Data: []byte("hello world")})
42 if err != nil {
43 t.Error(err)
44 }
45 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
46 ChunkId: "foo",
47 Offset: 0,
48 NumBytes: 2})
49 if err != nil {
50 t.Error(err)
51 }
52 if bytes.Compare(resp.Data, []byte("he")) != 0 {
53 t.Errorf("Expected: %s\nGot: %s\n", "he", resp.Data)
54 }
55 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
56 ChunkId: "foo",
57 Offset: 2,
58 NumBytes: 2})
59 if err != nil {
60 t.Error(err)
61 }
62 if bytes.Compare(resp.Data, []byte("ll")) != 0 {
63 t.Errorf("Expected: %s\nGot: %s\n", "ll", resp.Data)
64 }
65 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
66 ChunkId: "foo",
67 Offset: 4})
68 if err != nil {
69 t.Error(err)
70 }
71 if bytes.Compare(resp.Data, []byte("o world")) != 0 {
72 t.Errorf("Expected: %s\nGot: %s\n", "o world", resp.Data)
73 }
74
75}