| Giorgi Lekveishvili | 45b4d52 | 2020-03-19 21:11:18 +0400 | [diff] [blame^] | 1 | package chunk |
| 2 | |
| 3 | import "context" |
| 4 | import "bytes" |
| 5 | import "testing" |
| 6 | |
| 7 | import "pcloud/api" |
| 8 | |
| 9 | func TestStoreChunk(t *testing.T) { |
| 10 | s := NewChunkServer() |
| 11 | _, 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 | |
| 19 | func TestStoreAndReadChunk(t *testing.T) { |
| 20 | s := NewChunkServer() |
| 21 | _, 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 | |
| 37 | func TestReadWithOffsets(t *testing.T) { |
| 38 | s := NewChunkServer() |
| 39 | _, 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 | } |