@@ -3,11 +3,10 @@ package api
3
3
import (
4
4
"testing"
5
5
"time"
6
-
6
+
7
7
pb "github.com/Bit-Nation/panthalassa/api/pb"
8
- require "github.com/stretchr/testify/require"
9
8
proto "github.com/golang/protobuf/proto"
10
-
9
+ require "github.com/stretchr/testify/require"
11
10
)
12
11
13
12
type testUpStream struct {
@@ -23,59 +22,59 @@ func TestAPI_addAndCutRequestWorks(t *testing.T) {
23
22
24
23
req := pb.Request {}
25
24
req .RequestID = "hi"
26
-
25
+
27
26
// api
28
27
api := New (& testUpStream {})
29
-
28
+
30
29
// make sure request doesn't exist
31
30
_ , exist := api .requests ["hi" ]
32
31
require .False (t , exist )
33
-
32
+
34
33
api .addRequest (& req )
35
-
34
+
36
35
// make sure request does exist
37
36
_ , exist = api .requests ["hi" ]
38
37
require .True (t , exist )
39
-
38
+
40
39
// now cut request our of the stack and make sure it was removed
41
40
api .cutRequest ("hi" )
42
41
_ , exist = api .requests ["hi" ]
43
42
require .False (t , exist )
44
-
43
+
45
44
}
46
45
47
- func TestRequestResponse (t * testing.T ) {
48
-
46
+ func TestRequestResponse (t * testing.T ) {
47
+
49
48
dataChan := make (chan string )
50
-
49
+
51
50
var receivedRequestID string
52
-
51
+
53
52
// api
54
53
api := New (& testUpStream {
55
54
sendFn : func (data string ) {
56
55
dataChan <- data
57
56
},
58
57
})
59
-
58
+
60
59
go func () {
61
60
select {
62
- case data := <- dataChan :
63
- req := & pb.Request {}
64
- if err := proto .Unmarshal ([]byte (data ), req ); err != nil {
65
- panic (err )
66
- }
67
- receivedRequestID = req .RequestID
68
- out := api .Respond (req .RequestID , & pb.Response {
69
- RequestID : req .RequestID ,
70
- }, nil , time .Second )
71
- if out != nil {
72
- panic ("expected nil but got: " + out .Error ())
73
- }
61
+ case data := <- dataChan :
62
+ req := & pb.Request {}
63
+ if err := proto .Unmarshal ([]byte (data ), req ); err != nil {
64
+ panic (err )
65
+ }
66
+ receivedRequestID = req .RequestID
67
+ out := api .Respond (req .RequestID , & pb.Response {
68
+ RequestID : req .RequestID ,
69
+ }, nil , time .Second )
70
+ if out != nil {
71
+ panic ("expected nil but got: " + out .Error ())
72
+ }
74
73
}
75
74
}()
76
-
75
+
77
76
resp , err := api .request (& pb.Request {}, time .Second )
78
77
require .Nil (t , err )
79
78
require .Equal (t , resp .Msg .RequestID , receivedRequestID )
80
-
81
- }
79
+
80
+ }
0 commit comments