@@ -2,6 +2,11 @@ package gateway_test
22
33import (
44 "context"
5+ "strconv"
6+ "sync"
7+ "testing"
8+ "time"
9+
510 "github.com/graphql-go/graphql"
611 "github.com/stretchr/testify/require"
712 appsv1 "k8s.io/api/apps/v1"
@@ -10,10 +15,6 @@ import (
1015 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1116 "k8s.io/apimachinery/pkg/labels"
1217 "sigs.k8s.io/controller-runtime/pkg/client"
13- "strconv"
14- "sync"
15- "testing"
16- "time"
1718)
1819
1920func (suite * CommonTestSuite ) TestSchemaSubscribe () {
@@ -44,7 +45,7 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
4445 // this event will be received because we subscribed to replicas change.
4546 suite .updateDeployment (ctx , "my-new-deployment" , map [string ]string {"app" : "my-app" , "newLabel" : "changed" }, 2 )
4647 },
47- expectedEvents : 2 ,
48+ expectedEvents : 3 ,
4849 },
4950 {
5051 testName : "subscribe_to_deployments_by_labels_OK" ,
@@ -54,7 +55,7 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
5455 // this event will be ignored because we subscribe to deployment=first labels only
5556 suite .createDeployment (ctx , "my-second-deployment" , map [string ]string {"deployment" : "second" })
5657 },
57- expectedEvents : 1 ,
58+ expectedEvents : 2 ,
5859 },
5960 {
6061 testName : "subscribe_deployments_and_delete_deployment_OK" ,
@@ -63,7 +64,7 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
6364 suite .createDeployment (ctx , "my-new-deployment" , map [string ]string {"app" : "my-app" })
6465 suite .deleteDeployment (ctx , "my-new-deployment" )
6566 },
66- expectedEvents : 2 ,
67+ expectedEvents : 3 ,
6768 },
6869 {
6970 testName : "subscribeToClusterRole_OK" ,
@@ -79,7 +80,7 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
7980 setupFunc : func (ctx context.Context ) {
8081 suite .createClusterRole (ctx )
8182 },
82- expectedEvents : 65 ,
83+ expectedEvents : 66 ,
8384 },
8485 {
8586 testName : "incorrect_subscription_query" ,
@@ -107,6 +108,7 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
107108 wg := sync.WaitGroup {}
108109 wg .Add (tt .expectedEvents )
109110
111+ eventsReceived := 0
110112 go func () {
111113 for {
112114 select {
@@ -118,13 +120,19 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
118120 if tt .expectError && res .Errors == nil {
119121 t .Errorf ("Expected error but got nil" )
120122 cancel ()
123+ return
121124 }
122125
123126 if ! tt .expectError && res .Data == nil {
124127 t .Errorf ("Data is nil because of the error: %v" , res .Errors )
125128 cancel ()
129+ return
130+ }
131+
132+ eventsReceived ++
133+ if eventsReceived <= tt .expectedEvents {
134+ wg .Done ()
126135 }
127- wg .Done ()
128136
129137 case <- ctx .Done ():
130138 return
@@ -139,6 +147,9 @@ func (suite *CommonTestSuite) TestSchemaSubscribe() {
139147 }
140148
141149 wg .Wait ()
150+ if eventsReceived > tt .expectedEvents {
151+ t .Errorf ("Received more events than expected. Expected: %d, Got: %d" , tt .expectedEvents , eventsReceived )
152+ }
142153 })
143154 }
144155}
0 commit comments