blob: 70f209f6100e5d5dd8860343a116dcc83fe2891a [file] [log] [blame]
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +04001package chunk
2
giolekvac5126d92020-03-21 16:39:56 +04003import (
4 "bytes"
5 "context"
6 "testing"
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +04007
giolekvac5126d92020-03-21 16:39:56 +04008 "pcloud/api"
9)
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040010
11func TestStoreChunk(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040012 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040013 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
14 ChunkId: "foo",
15 Data: []byte("hello world")})
16 if err != nil {
17 t.Error(err)
18 }
19}
20
21func TestStoreAndReadChunk(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040022 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040023 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
24 ChunkId: "foo",
25 Data: []byte("hello world")})
26 if err != nil {
27 t.Error(err)
28 }
29 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
giolekva1f6577a2020-03-25 12:53:06 +040030 ChunkId: "foo",
31 NumBytes: 100})
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040032 if err != nil {
33 t.Error(err)
34 }
35 if bytes.Compare(resp.Data, []byte("hello world")) != 0 {
36 t.Errorf("Expected: %s\nGot: %s\n", "hello world", resp.Data)
37 }
38}
39
40func TestReadWithOffsets(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040041 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040042 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
43 ChunkId: "foo",
44 Data: []byte("hello world")})
45 if err != nil {
46 t.Error(err)
47 }
48 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
49 ChunkId: "foo",
50 Offset: 0,
51 NumBytes: 2})
52 if err != nil {
53 t.Error(err)
54 }
55 if bytes.Compare(resp.Data, []byte("he")) != 0 {
56 t.Errorf("Expected: %s\nGot: %s\n", "he", resp.Data)
57 }
58 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
59 ChunkId: "foo",
60 Offset: 2,
61 NumBytes: 2})
62 if err != nil {
63 t.Error(err)
64 }
65 if bytes.Compare(resp.Data, []byte("ll")) != 0 {
66 t.Errorf("Expected: %s\nGot: %s\n", "ll", resp.Data)
67 }
68 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
giolekva1f6577a2020-03-25 12:53:06 +040069 ChunkId: "foo",
70 Offset: 4,
71 NumBytes: 100})
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040072 if err != nil {
73 t.Error(err)
74 }
75 if bytes.Compare(resp.Data, []byte("o world")) != 0 {
76 t.Errorf("Expected: %s\nGot: %s\n", "o world", resp.Data)
77 }
78
79}