@@ -31,15 +31,27 @@ func TestCustomConnectionFunction(t *testing.T) {
31
31
netClient , netServer := net .Pipe ()
32
32
defer netClient .Close ()
33
33
defer netServer .Close ()
34
- var firstMessage = ""
34
+
35
+ outputChan := make (chan struct {
36
+ msg []byte
37
+ err error
38
+ })
35
39
go func () {
36
40
// read first message only
37
41
bytes := make ([]byte , 1024 )
42
+ netServer .SetDeadline (time .Now ().Add (time .Second )) // Ensure this will always complete
38
43
n , err := netServer .Read (bytes )
39
44
if err != nil {
40
- t .Errorf ("%v" , err )
45
+ outputChan <- struct {
46
+ msg []byte
47
+ err error
48
+ }{err : err }
49
+ } else {
50
+ outputChan <- struct {
51
+ msg []byte
52
+ err error
53
+ }{msg : bytes [:n ]}
41
54
}
42
- firstMessage = string (bytes [:n ])
43
55
}()
44
56
// Set custom network connection function and client connect
45
57
var customConnectionFunc OpenConnectionFunc = func (uri * url.URL , options ClientOptions ) (net.Conn , error ) {
@@ -52,10 +64,16 @@ func TestCustomConnectionFunction(t *testing.T) {
52
64
53
65
// Try to connect using custom function, wait for 2 seconds, to pass MQTT first message
54
66
if token := client .Connect (); token .WaitTimeout (2 * time .Second ) && token .Error () != nil {
55
- t .Errorf ("%v" , token .Error ())
67
+ t .Fatalf ("%v" , token .Error ())
68
+ }
69
+
70
+ msg := <- outputChan
71
+ if msg .err != nil {
72
+ t .Fatalf ("read from simulated connection failed: %v" , msg .err )
56
73
}
57
74
58
75
// Analyze first message sent by client and received by the server
76
+ firstMessage := string (msg .msg )
59
77
if len (firstMessage ) <= 0 || ! strings .Contains (firstMessage , "MQTT" ) {
60
78
t .Error ("no message received on connect" )
61
79
}
0 commit comments