@@ -19,6 +19,7 @@ package discovery
19
19
20
20
import (
21
21
"fmt"
22
+ "io"
22
23
"net"
23
24
"testing"
24
25
"time"
@@ -93,3 +94,58 @@ func TestDiscoveryStdioHandling(t *testing.T) {
93
94
94
95
require .False (t , disc .Alive ())
95
96
}
97
+
98
+ func TestClient (t * testing.T ) {
99
+ // Build dummy-discovery
100
+ builder , err := paths .NewProcess (nil , "go" , "build" )
101
+ require .NoError (t , err )
102
+ builder .SetDir ("dummy-discovery" )
103
+ require .NoError (t , builder .Run ())
104
+
105
+ t .Run ("WithDiscoveryCrashingOnStartup" , func (t * testing.T ) {
106
+ // Run client with discovery crashing on startup
107
+ cl := NewClient ("1" , "dummy-discovery/dummy-discovery" , "--invalid" )
108
+ require .ErrorIs (t , cl .Run (), io .EOF )
109
+ })
110
+
111
+ t .Run ("WithDiscoveryCrashingWhileSendingCommands" , func (t * testing.T ) {
112
+ // Run client with crashing discovery after 1 second
113
+ cl := NewClient ("1" , "dummy-discovery/dummy-discovery" , "-k" )
114
+ require .NoError (t , cl .Run ())
115
+
116
+ time .Sleep (time .Second )
117
+
118
+ ch , err := cl .StartSync (20 )
119
+ require .Error (t , err )
120
+ require .Nil (t , ch )
121
+ })
122
+
123
+ t .Run ("WithDiscoveryCrashingWhileStreamingEvents" , func (t * testing.T ) {
124
+ // Run client with crashing discovery after 1 second
125
+ cl := NewClient ("1" , "dummy-discovery/dummy-discovery" , "-k" )
126
+ require .NoError (t , cl .Run ())
127
+
128
+ ch , err := cl .StartSync (20 )
129
+ require .NoError (t , err )
130
+
131
+ time .Sleep (time .Second )
132
+
133
+ loop:
134
+ for {
135
+ select {
136
+ case msg , ok := <- ch :
137
+ if ! ok {
138
+ // Channel closed: Test passed
139
+ fmt .Println ("Event channel closed" )
140
+ break loop
141
+ }
142
+ fmt .Println ("Recv: " , msg )
143
+ case <- time .After (time .Second ):
144
+ t .Error ("Crashing client did not close event channel" )
145
+ break loop
146
+ }
147
+ }
148
+
149
+ cl .Quit ()
150
+ })
151
+ }
0 commit comments