@@ -20,26 +20,22 @@ import (
2020)
2121
2222func TestEventSourceSimpleFlow (t * testing.T ) {
23- es := createEventSource (t , "" , nil , nil )
24-
2523 messageCounter := 0
2624 messageFunc := func (e any ) {
2725 event := e .(* Event )
2826 assertEqual (t , strconv .Itoa (messageCounter ), event .ID )
2927 assertEqual (t , true , strings .HasPrefix (event .Data , "The time is" ))
3028 messageCounter ++
31- if messageCounter == 100 {
32- es .Close ()
33- }
3429 }
35- es .OnMessage (messageFunc , nil )
3630
3731 counter := 0
32+ es := createEventSource (t , "" , messageFunc , nil )
3833 ts := createSSETestServer (
3934 t ,
4035 10 * time .Millisecond ,
4136 func (w io.Writer ) error {
4237 if counter == 100 {
38+ es .Close ()
4339 return fmt .Errorf ("stop sending events" )
4440 }
4541 _ , err := fmt .Fprintf (w , "id: %v\n data: The time is %s\n \n " , counter , time .Now ().Format (time .UnixDate ))
@@ -133,25 +129,22 @@ func TestEventSourceOverwriteFuncs(t *testing.T) {
133129 messageFunc1 := func (e any ) {
134130 assertNotNil (t , e )
135131 }
136- es := createEventSource (t , "" , messageFunc1 , nil )
137-
138132 message2Counter := 0
139133 messageFunc2 := func (e any ) {
140134 event := e .(* Event )
141135 assertEqual (t , strconv .Itoa (message2Counter ), event .ID )
142136 assertEqual (t , true , strings .HasPrefix (event .Data , "The time is" ))
143137 message2Counter ++
144- if message2Counter == 50 {
145- es .Close ()
146- }
147138 }
148139
149140 counter := 0
141+ es := createEventSource (t , "" , messageFunc1 , nil )
150142 ts := createSSETestServer (
151143 t ,
152144 10 * time .Millisecond ,
153145 func (w io.Writer ) error {
154146 if counter == 50 {
147+ es .Close ()
155148 return fmt .Errorf ("stop sending events" )
156149 }
157150 _ , err := fmt .Fprintf (w , "id: %v\n data: The time is %s\n \n " , counter , time .Now ().Format (time .UnixDate ))
@@ -184,21 +177,16 @@ func TestEventSourceOverwriteFuncs(t *testing.T) {
184177}
185178
186179func TestEventSourceRetry (t * testing.T ) {
187- es := createEventSource (t , "" , nil , nil )
188-
189180 messageCounter := 2 // 0 & 1 connection failure
190181 messageFunc := func (e any ) {
191182 event := e .(* Event )
192183 assertEqual (t , strconv .Itoa (messageCounter ), event .ID )
193184 assertEqual (t , true , strings .HasPrefix (event .Data , "The time is" ))
194185 messageCounter ++
195- if messageCounter == 15 {
196- es .Close ()
197- }
198186 }
199- es .OnMessage (messageFunc , nil )
200187
201188 counter := 0
189+ es := createEventSource (t , "" , messageFunc , nil )
202190 ts := createTestServer (func (w http.ResponseWriter , r * http.Request ) {
203191 if counter == 1 && r .URL .Query ().Get ("reconnect" ) == "1" {
204192 w .WriteHeader (http .StatusTooManyRequests )
@@ -457,24 +445,19 @@ func TestEventSourceWithDifferentMethods(t *testing.T) {
457445
458446 for _ , tc := range testCases {
459447 t .Run (tc .name , func (t * testing.T ) {
460- es := createEventSource (t , "" , nil , nil )
461-
462448 messageCounter := 0
463449 messageFunc := func (e any ) {
464450 event := e .(* Event )
465451 assertEqual (t , strconv .Itoa (messageCounter ), event .ID )
466452 assertEqual (t , true , strings .HasPrefix (event .Data , fmt .Sprintf ("%s method test:" , tc .method )))
467453 messageCounter ++
468- if messageCounter == 20 {
469- es .Close ()
470- }
471454 }
472- es .OnMessage (messageFunc , nil )
473455
474456 counter := 0
475457 methodVerified := false
476458 bodyVerified := false
477459
460+ es := createEventSource (t , "" , messageFunc , nil )
478461 ts := createMethodVerifyingSSETestServer (
479462 t ,
480463 10 * time .Millisecond ,
@@ -484,6 +467,7 @@ func TestEventSourceWithDifferentMethods(t *testing.T) {
484467 & bodyVerified ,
485468 func (w io.Writer ) error {
486469 if counter == 20 {
470+ es .Close ()
487471 return fmt .Errorf ("stop sending events" )
488472 }
489473 _ , err := fmt .Fprintf (w , "id: %v\n data: %s method test: %s\n \n " , counter , tc .method , time .Now ().Format (time .RFC3339 ))
0 commit comments