-
Notifications
You must be signed in to change notification settings - Fork 123
/
future_test.go
167 lines (138 loc) · 3.58 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// REVU - whitebox testing of internal comps -- OK.
package redis
import (
"bytes"
"log"
"testing"
"time"
)
// REVU: nice to test timing accuracy but it isn't really necessary.
// basic test data
type tspec_ft struct {
data []byte
delay time.Duration
}
// create the test spec (test data for now)
func testspec_ft() tspec_ft {
var testspec tspec_ft
// []byte data to be used
data := "Hello there!"
testspec.data = bytes.NewBufferString(data).Bytes()
// using a timeout of 100 (msecs)
delaysecs := 100
testspec.delay = time.Duration(delaysecs) * time.Millisecond
return testspec
}
// test FutureBytes - all aspect, expect blocking Get
// are tested. A future object is created (but not set)
// and timeout and error tests are made. Then value is set
// and test repeated.
func TestFutureContract(t *testing.T) {
// prep data
tspec := testspec_ft()
// using basic FutureBytes
fb := newFutureBytes()
/* TEST timed call to uninitialized (not Set) future value,
* expecting a timeout.
* MUST return timeout of true
* MUST return error value of nil
* MUST return value of nil
*/
fvalue1, e1, timedout1 := fb.TryGet(tspec.delay)
if !timedout1 {
t.Error("BUG: timeout expected")
}
if e1 != nil {
t.Error("Bug: unexpected error %s", e1)
}
if fvalue1 != nil {
t.Error("Bug: value returned: %s", fvalue1)
}
// set the future result
fb.set(tspec.data)
/* TEST timed call to initialized (set) future value
* expecting data, no error and no timeout
* MUST return timeout of false
* MUST return error of nil
* MUST return value equal to data
*/
fvalue2, e2, timedout2 := fb.TryGet(tspec.delay)
if timedout2 {
t.Error("BUG: should not timeout")
}
if e2 != nil {
t.Error("Bug: unexpected error %s", e2)
}
if fvalue2 == nil {
t.Error("Bug: should not return future nil")
} else if bytes.Compare(fvalue2, tspec.data) != 0 {
t.Error("Bug: future value not equal to data set")
}
}
func TestFutureWithBlockingGet(t *testing.T) {
// prep data
// prep data
tspec := testspec_ft()
// using basic FutureBytes
fb := newFutureBytes()
// test go routine will block on Get until
// value is set.
sig := make(chan bool, 1)
go func() {
/* TEST timed call to initialized (set) future value
* expecting data, no error and no timeout
* MUST return error of nil
* MUST return value equal to data
*/
fvalue, e := fb.Get()
if e != nil {
t.Error("Bug: unexpected error %s", e)
}
if fvalue == nil {
t.Error("Bug: should not return future nil")
} else if bytes.Compare(fvalue, tspec.data) != 0 {
t.Error("Bug: future value not equal to data set")
}
sig <- true
}()
// set the data
fb.set(tspec.data)
<-sig
}
func TestFutureTimedBlockingGet(t *testing.T) {
// prep data
// prep data
tspec := testspec_ft()
// using basic FutureBytes
fb := newFutureBytes()
// test go routine will block on Get until
// value is set or timeout expires
sig := make(chan bool, 1)
go func() {
/* TEST timed call to initialized (set) future value
* expecting data, no error and no timeout
* MUST return error of nil
* MUST return value equal to data
*/
fvalue, e, timedout := fb.TryGet(tspec.delay)
if timedout {
t.Error("BUG: should not timeout")
}
if e != nil {
t.Error("Bug: unexpected error %s", e)
}
if fvalue == nil {
t.Error("Bug: should not return future nil")
} else if bytes.Compare(fvalue, tspec.data) != 0 {
t.Error("Bug: future value not equal to data set")
}
sig <- true
}()
// set the data
fb.set(tspec.data)
<-sig
}
func TestEnd_future(t *testing.T) {
// nop
log.Println("-- future test completed")
}