-
Notifications
You must be signed in to change notification settings - Fork 1
/
future_test.go
86 lines (71 loc) · 1.76 KB
/
future_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
package raft_test
import (
"testing"
"time"
"github.com/relab/raft"
"github.com/relab/raft/commonpb"
)
func TestNewPromiseNoFuture(t *testing.T) {
promise := raft.NewPromiseNoFuture(nil)
promise.Respond(nil)
}
func TestNewPromiseLogEntryWrite(t *testing.T) {
var index uint64 = 1
value := "res"
entry := &commonpb.Entry{}
promise, future := raft.NewPromiseEntry(entry)
logPromise := promise.Write(index)
logPromise.Respond(value)
time.Sleep(time.Millisecond)
select {
case res := <-future.ResultCh():
s, ok := res.Value.(string)
if !ok {
t.Error("expected type string")
}
if s != value {
t.Errorf("got %s, want %s", s, value)
}
if res.Index != index {
t.Errorf("got %d, want %d", res.Index, index)
}
default:
t.Errorf("expected read from future")
}
if logPromise.Duration() < time.Millisecond {
t.Errorf("expected at least %v duration", time.Millisecond)
}
if logPromise.Entry() != entry {
t.Errorf("got %+v, expected %+v", logPromise.Entry(), entry)
}
}
func TestNewPromiseLogEntryRead(t *testing.T) {
var index uint64
value := "res"
entry := &commonpb.Entry{}
promise, future := raft.NewPromiseEntry(entry)
readPromise := promise.Read()
readPromise.Respond(value)
time.Sleep(time.Millisecond)
select {
case res := <-future.ResultCh():
s, ok := res.Value.(string)
if !ok {
t.Error("expected type string")
}
if s != value {
t.Errorf("got %s, want %s", s, value)
}
if res.Index != index {
t.Errorf("got %d, want %d", res.Index, index)
}
default:
t.Errorf("expected read from future")
}
if readPromise.Duration() < time.Millisecond {
t.Errorf("expected at least %v duration", time.Millisecond)
}
if readPromise.Entry() != entry {
t.Errorf("got %+v, expected %+v", readPromise.Entry(), entry)
}
}