Debian Codegraph @

protocolserver_test.go

raw
1 package desync
2
3 import (
4 "context"
5 "io"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestProtocolServer(t *testing.T) {
12 r1, w1 := io.Pipe()
13 r2, w2 := io.Pipe()
14
15 server := NewProtocol(r1, w2)
16
17 // Test data
18 uncompressed := []byte{4, 3, 2, 1}
19 chunkIn := NewChunk(uncompressed)
20 id := chunkIn.ID()
21 store := &TestStore{}
22 store.StoreChunk(chunkIn)
23
24 ps := NewProtocolServer(r2, w1, store)
25
26 go ps.Serve(context.Background())
27
28 // Client
29 flags, err := server.Initialize(CaProtocolPullChunks)
30 require.NoError(t, err)
31 require.NotZero(t, flags&CaProtocolReadableStore, "server not offering chunks")
32
33 // Should find this chunk
34 chunk, err := server.RequestChunk(id)
35 require.NoError(t, err)
36 b, _ := chunk.Data()
37 require.Equal(t, uncompressed, b)
38
39 // This one's missing
40 _, err = server.RequestChunk(ChunkID{0})
41 require.IsType(t, ChunkMissing{}, err)
42 }