15
15
package ovsdb_test
16
16
17
17
import (
18
- "encoding/json"
19
18
"fmt"
20
- "net"
21
- "sync"
22
19
"testing"
23
20
24
21
"github.com/digitalocean/go-openvswitch/ovsdb"
22
+ "github.com/digitalocean/go-openvswitch/ovsdb/internal/jsonrpc"
25
23
"github.com/google/go-cmp/cmp"
26
24
)
27
25
28
26
func TestClientError (t * testing.T ) {
29
27
const str = "some error"
30
28
31
- c , done := testClient (t , func (_ string , _ []interface {}) interface {} {
32
- return & ovsdb.Error {
33
- Err : str ,
34
- Details : "malformed" ,
35
- Syntax : "{}" ,
29
+ c , done := testClient (t , func (_ jsonrpc.Request ) jsonrpc.Response {
30
+ return jsonrpc.Response {
31
+ ID : 1 ,
32
+ Result : & ovsdb.Error {
33
+ Err : str ,
34
+ Details : "malformed" ,
35
+ Syntax : "{}" ,
36
+ },
36
37
}
37
38
})
38
39
defer done ()
@@ -51,19 +52,23 @@ func TestClientError(t *testing.T) {
51
52
t .Fatalf ("unexpected error (-want +got):\n %s" , diff )
52
53
}
53
54
}
55
+
54
56
func TestClientListDatabases (t * testing.T ) {
55
57
want := []string {"Open_vSwitch" , "test" }
56
58
57
- c , done := testClient (t , func (method string , params [] interface {}) interface {} {
58
- if diff := cmp .Diff ("list_dbs" , method ); diff != "" {
59
- t . Fatalf ("unexpected RPC method (-want +got):\n %s" , diff )
59
+ c , done := testClient (t , func (req jsonrpc. Request ) jsonrpc. Response {
60
+ if diff := cmp .Diff ("list_dbs" , req . Method ); diff != "" {
61
+ panicf ("unexpected RPC method (-want +got):\n %s" , diff )
60
62
}
61
63
62
- if diff := cmp .Diff (1 , len (params )); diff != "" {
63
- t . Fatalf ("unexpected number of RPC parameters (-want +got):\n %s" , diff )
64
+ if diff := cmp .Diff (0 , len (req . Params )); diff != "" {
65
+ panicf ("unexpected number of RPC parameters (-want +got):\n %s" , diff )
64
66
}
65
67
66
- return want
68
+ return jsonrpc.Response {
69
+ ID : 1 ,
70
+ Result : want ,
71
+ }
67
72
})
68
73
defer done ()
69
74
@@ -77,70 +82,17 @@ func TestClientListDatabases(t *testing.T) {
77
82
}
78
83
}
79
84
80
- type rpcFunc func (method string , params []interface {}) interface {}
81
-
82
- func testClient (t * testing.T , fn rpcFunc ) (* ovsdb.Client , func ()) {
85
+ func testClient (t * testing.T , fn jsonrpc.TestFunc ) (* ovsdb.Client , func ()) {
83
86
t .Helper ()
84
87
85
- l , err := net .Listen ("tcp" , ":0" )
86
- if err != nil {
87
- t .Fatalf ("failed to listen: %v" , err )
88
- }
89
-
90
- var wg sync.WaitGroup
91
- wg .Add (1 )
92
-
93
- go func () {
94
- defer wg .Done ()
95
-
96
- // Accept a single connection.
97
- c , err := l .Accept ()
98
- if err != nil {
99
- panicf ("failed to accept: %v" , err )
100
- }
101
- defer c .Close ()
102
- _ = l .Close ()
103
-
104
- if err := handleConn (c , fn ); err != nil {
105
- panicf ("failed to handle connection: %v" , err )
106
- }
107
- }()
88
+ conn , done := jsonrpc .TestNetConn (t , fn )
108
89
109
- c , err := ovsdb .Dial ( "tcp" , l . Addr (). String () )
90
+ c , err := ovsdb .New ( conn )
110
91
if err != nil {
111
92
t .Fatalf ("failed to dial: %v" , err )
112
93
}
113
94
114
- return c , func () {
115
- // Ensure types are cleaned up, and ensure goroutine stops.
116
- _ = l .Close ()
117
- _ = c .Close ()
118
- wg .Wait ()
119
- }
120
- }
121
-
122
- func handleConn (c net.Conn , fn rpcFunc ) error {
123
- var req struct {
124
- Method string `json:"method"`
125
- Params []interface {} `json:"params"`
126
- ID int `json:"id"`
127
- }
128
-
129
- var res struct {
130
- Result interface {} `json:"result"`
131
- ID int `json:"id"`
132
- }
133
-
134
- if err := json .NewDecoder (c ).Decode (& req ); err != nil {
135
- return err
136
- }
137
-
138
- result := fn (req .Method , req .Params )
139
-
140
- res .ID = req .ID
141
- res .Result = result
142
-
143
- return json .NewEncoder (c ).Encode (res )
95
+ return c , done
144
96
}
145
97
146
98
func panicf (format string , a ... interface {}) {
0 commit comments