blob: 2c6fb832c14a50bba7fcb164c8afbfca80d6d566 [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{}}
giolekva95c6cfe2020-03-25 20:23:30 +040013 _, err := s.CreateChunk(context.Background(), &api.CreateChunkRequest{
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040014 ChunkId: "foo",
giolekva95c6cfe2020-03-25 20:23:30 +040015 Size: 11,
16 Role: api.ReplicaRole_PRIMARY})
17 if err != nil {
18 t.Error(err)
19 }
20 _, err = s.WriteChunk(context.Background(), &api.WriteChunkRequest{
21 ChunkId: "foo",
22 Offset: 0,
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040023 Data: []byte("hello world")})
24 if err != nil {
25 t.Error(err)
26 }
27}
28
29func TestStoreAndReadChunk(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040030 s := ChunkServer{factory: &InMemoryChunkFactory{}}
giolekva95c6cfe2020-03-25 20:23:30 +040031 _, err := s.CreateChunk(context.Background(), &api.CreateChunkRequest{
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040032 ChunkId: "foo",
giolekva95c6cfe2020-03-25 20:23:30 +040033 Size: 11,
34 Role: api.ReplicaRole_PRIMARY})
35 if err != nil {
36 t.Error(err)
37 }
38 _, err = s.WriteChunk(context.Background(), &api.WriteChunkRequest{
39 ChunkId: "foo",
40 Offset: 0,
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040041 Data: []byte("hello world")})
42 if err != nil {
43 t.Error(err)
44 }
45 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
giolekva1f6577a2020-03-25 12:53:06 +040046 ChunkId: "foo",
47 NumBytes: 100})
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040048 if err != nil {
49 t.Error(err)
50 }
51 if bytes.Compare(resp.Data, []byte("hello world")) != 0 {
52 t.Errorf("Expected: %s\nGot: %s\n", "hello world", resp.Data)
53 }
54}
55
56func TestReadWithOffsets(t *testing.T) {
giolekva7be17df2020-03-21 13:57:02 +040057 s := ChunkServer{factory: &InMemoryChunkFactory{}}
giolekva95c6cfe2020-03-25 20:23:30 +040058 _, err := s.CreateChunk(context.Background(), &api.CreateChunkRequest{
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040059 ChunkId: "foo",
giolekva95c6cfe2020-03-25 20:23:30 +040060 Size: 11,
61 Role: api.ReplicaRole_PRIMARY})
62 if err != nil {
63 t.Error(err)
64 }
65 _, err = s.WriteChunk(context.Background(), &api.WriteChunkRequest{
66 ChunkId: "foo",
67 Offset: 0,
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040068 Data: []byte("hello world")})
69 if err != nil {
70 t.Error(err)
71 }
72 resp, err := s.ReadChunk(context.Background(), &api.ReadChunkRequest{
73 ChunkId: "foo",
74 Offset: 0,
75 NumBytes: 2})
76 if err != nil {
77 t.Error(err)
78 }
79 if bytes.Compare(resp.Data, []byte("he")) != 0 {
80 t.Errorf("Expected: %s\nGot: %s\n", "he", resp.Data)
81 }
82 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
83 ChunkId: "foo",
84 Offset: 2,
85 NumBytes: 2})
86 if err != nil {
87 t.Error(err)
88 }
89 if bytes.Compare(resp.Data, []byte("ll")) != 0 {
90 t.Errorf("Expected: %s\nGot: %s\n", "ll", resp.Data)
91 }
92 resp, err = s.ReadChunk(context.Background(), &api.ReadChunkRequest{
giolekva1f6577a2020-03-25 12:53:06 +040093 ChunkId: "foo",
94 Offset: 4,
95 NumBytes: 100})
Giorgi Lekveishvili45b4d522020-03-19 21:11:18 +040096 if err != nil {
97 t.Error(err)
98 }
99 if bytes.Compare(resp.Data, []byte("o world")) != 0 {
100 t.Errorf("Expected: %s\nGot: %s\n", "o world", resp.Data)
101 }
102
103}