-
Notifications
You must be signed in to change notification settings - Fork 44
/
protocolserver_test.go
51 lines (42 loc) · 971 Bytes
/
protocolserver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package desync
import (
"bytes"
"context"
"io"
"testing"
)
func TestProtocolServer(t *testing.T) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
server := NewProtocol(r1, w2)
// Test data
uncompressed := []byte{4, 3, 2, 1}
chunkIn := NewChunk(uncompressed)
id := chunkIn.ID()
store := &TestStore{}
store.StoreChunk(chunkIn)
ps := NewProtocolServer(r2, w1, store)
go ps.Serve(context.Background())
// Client
flags, err := server.Initialize(CaProtocolPullChunks)
if err != nil {
t.Fatal(err)
}
if flags&CaProtocolReadableStore == 0 {
t.Fatalf("server not offering chunks")
}
// Should find this chunk
chunk, err := server.RequestChunk(id)
if err != nil {
t.Fatal(err)
}
b, _ := chunk.Data()
if !bytes.Equal(b, uncompressed) {
t.Fatal("chunk data doesn't match expected")
}
// This one's missing
_, err = server.RequestChunk(ChunkID{0})
if _, ok := err.(ChunkMissing); !ok {
t.Fatal("expected ChunkMissing error, got:", err)
}
}