Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit 11be0b1

Browse files
authored
feat: send empty array if no resources in subscription (#239)
* feat: send empty array if no resources in subscription * fix: simple solution for empty array * fix: fixed subscription_test
1 parent 9d51409 commit 11be0b1

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

gateway/resolver/subscription.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package resolver
22

33
import (
44
"fmt"
5-
"github.com/openmfp/golang-commons/sentry"
65
"reflect"
76
"sort"
87
"strings"
98

9+
"github.com/openmfp/golang-commons/sentry"
10+
1011
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1112

1213
"github.com/graphql-go/graphql/language/ast"
@@ -119,6 +120,14 @@ func (r *Service) runWatch(
119120
return
120121
}
121122

123+
if !singleItem {
124+
select {
125+
case <-ctx.Done():
126+
return
127+
case resultChannel <- []map[string]interface{}{}:
128+
}
129+
}
130+
122131
watcher, err := r.runtimeClient.Watch(ctx, list, opts...)
123132
if err != nil {
124133
r.log.Error().Err(err).Str("gvk", gvk.String()).Msg("Failed to start watch")

tests/gateway_test/subscription_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ package gateway_test
22

33
import (
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

1920
func (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

Comments
 (0)