blob: f57a9255b1205bebf6cfd4031850419087e9ee25 [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{
30 ChunkId: "foo"})
31 if err != nil {
32 t.Error(err)
33 }
34 if bytes.Compare(resp.Data, []byte("hello world")) != 0 {
35 t.Errorf("Expected: %s\nGot: %s\n", "hello world", resp.Data)
36 }
37}
38
39func TestReadWithOffsets(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040040 s := ChunkServer{factory: &InMemoryChunkFactory{}}
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040041 _, err := s.StoreChunk(context.Background(), &api.StoreChunkRequest{
42 ChunkId: "foo",
43 Data: []byte("hello world")})
44 if err != nil {
45 t.Error(err)
46 }
47 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
48 ChunkId: "foo",
49 Offset: 0,
50 NumBytes: 2})
51 if err != nil {
52 t.Error(err)
53 }
54 if bytes.Compare(resp.Data, []byte("he")) != 0 {
55 t.Errorf("Expected: %s\nGot: %s\n", "he", resp.Data)
56 }
57 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
58 ChunkId: "foo",
59 Offset: 2,
60 NumBytes: 2})
61 if err != nil {
62 t.Error(err)
63 }
64 if bytes.Compare(resp.Data, []byte("ll")) != 0 {
65 t.Errorf("Expected: %s\nGot: %s\n", "ll", resp.Data)
66 }
67 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
68 ChunkId: "foo",
69 Offset: 4})
70 if err != nil {
71 t.Error(err)
72 }
73 if bytes.Compare(resp.Data, []byte("o world")) != 0 {
74 t.Errorf("Expected: %s\nGot: %s\n", "o world", resp.Data)
75 }
76
77}