-
Notifications
You must be signed in to change notification settings - Fork 994
/
inmem_transport_test.go
87 lines (78 loc) · 2.06 KB
/
inmem_transport_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package raft
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestInmemTransportImpl(t *testing.T) {
var inm interface{} = &InmemTransport{}
if _, ok := inm.(Transport); !ok {
t.Fatalf("InmemTransport is not a Transport")
}
if _, ok := inm.(LoopbackTransport); !ok {
t.Fatalf("InmemTransport is not a Loopback Transport")
}
if _, ok := inm.(WithPeers); !ok {
t.Fatalf("InmemTransport is not a WithPeers Transport")
}
}
func TestInmemTransportWriteTimeout(t *testing.T) {
// InmemTransport should timeout if the other end has gone away
// when it tries to send a request.
// Use unbuffered channels so that we can see the write failing
// without having to contrive to fill up the buffer first.
timeout := 10 * time.Millisecond
t1 := &InmemTransport{
consumerCh: make(chan RPC),
localAddr: NewInmemAddr(),
peers: make(map[ServerAddress]*InmemTransport),
timeout: timeout,
}
t2 := &InmemTransport{
consumerCh: make(chan RPC),
localAddr: NewInmemAddr(),
peers: make(map[ServerAddress]*InmemTransport),
timeout: timeout,
}
a2 := t2.LocalAddr()
t1.Connect(a2, t2)
stop := make(chan struct{})
stopped := make(chan struct{})
go func() {
defer close(stopped)
var i uint64
for {
select {
case <-stop:
return
case rpc := <-t2.Consumer():
i++
rpc.Respond(&AppendEntriesResponse{
Success: true,
LastLog: i,
}, nil)
}
}
}()
var resp AppendEntriesResponse
// Sanity check that sending is working before stopping the
// responder.
err := t1.AppendEntries("server1", a2, &AppendEntriesRequest{}, &resp)
NoErr(err, t)
require.True(t, resp.LastLog == 1)
close(stop)
select {
case <-stopped:
case <-time.After(time.Second):
t.Fatalf("timed out waiting for responder to stop")
}
err = t1.AppendEntries("server1", a2, &AppendEntriesRequest{}, &resp)
if err == nil {
t.Fatalf("expected AppendEntries to time out")
}
if err.Error() != "send timed out" {
t.Fatalf("unexpected error: %v", err)
}
}