-
Notifications
You must be signed in to change notification settings - Fork 44
/
protocol_test.go
69 lines (61 loc) · 1.29 KB
/
protocol_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package desync
import (
"bytes"
"io"
"testing"
)
func TestProtocol(t *testing.T) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
server := NewProtocol(r1, w2)
client := NewProtocol(r2, w1)
// Test data
uncompressed := []byte{0, 0, 1, 1, 2, 2}
inChunk := NewChunk(uncompressed)
compressed, _ := Compressor{}.toStorage(uncompressed)
cID := inChunk.ID()
// Server
go func() {
flags, err := client.Initialize(CaProtocolReadableStore)
if err != nil {
t.Fatal(err)
}
if flags&CaProtocolPullChunks == 0 {
t.Fatalf("client not asking for chunks")
}
for {
m, err := client.ReadMessage()
if err != nil {
t.Fatal(err)
}
switch m.Type {
case CaProtocolRequest:
id, err := ChunkIDFromSlice(m.Body[8:40])
if err != nil {
t.Fatal(err)
}
if err := client.SendProtocolChunk(id, 0, compressed); err != nil {
t.Fatal(err)
}
default:
t.Fatal("unexpected message")
}
}
}()
// Client
flags, err := server.Initialize(CaProtocolPullChunks)
if err != nil {
t.Fatal(err)
}
if flags&CaProtocolReadableStore == 0 {
t.Fatalf("server not offering chunks")
}
chunk, err := server.RequestChunk(cID)
if err != nil {
t.Fatal(err)
}
b, _ := chunk.Data()
if !bytes.Equal(b, uncompressed) {
t.Fatal("chunk data doesn't match expected")
}
}