Skip to content

Commit 679cad4

Browse files
committed
test: fix CreateAndDeleteResource for DiscoveryResourceBatchObservable device
1 parent 0285bd9 commit 679cad4

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

grpc-gateway/service/createAndDeleteResource_test.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,22 @@ func deleteSwitchResourceExpectedEvents(t *testing.T, deviceID, subID, correlati
207207
}
208208
}
209209

210-
func validateEvents(t *testing.T, subClient pb.GrpcGateway_SubscribeToEventsClient, expectedEvents map[string]*pb.Event, failOnUnexpectedEvent bool) {
210+
func batchDeleteSwitchChangedResourceExpected(t *testing.T, deviceID, subID, correlationID, switchID string) map[string]*pb.Event {
211+
212+
changed := &pb.Event{
213+
SubscriptionId: subID,
214+
CorrelationId: correlationID,
215+
Type: &pb.Event_ResourceChanged{
216+
ResourceChanged: pbTest.MakeResourceChanged(t, deviceID, test.TestResourceSwitchesInstanceHref(switchID), "", map[interface{}]interface{}{}),
217+
},
218+
}
219+
220+
return map[string]*pb.Event{
221+
pbTest.GetEventID(changed): changed,
222+
}
223+
}
224+
225+
func validateEvents(t *testing.T, subClient pb.GrpcGateway_SubscribeToEventsClient, expectedEvents map[string]*pb.Event) {
211226
for {
212227
ev, err := subClient.Recv()
213228
if kitNetGrpc.IsContextDeadlineExceeded(err) {
@@ -218,10 +233,6 @@ func validateEvents(t *testing.T, subClient pb.GrpcGateway_SubscribeToEventsClie
218233
eventID := pbTest.GetEventID(ev)
219234
expected, ok := expectedEvents[eventID]
220235
if !ok {
221-
if !failOnUnexpectedEvent {
222-
t.Logf("unexpected event received: %+v", ev)
223-
continue
224-
}
225236
require.Failf(t, "unexpected event", "invalid event: %+v", ev)
226237
}
227238
pbTest.CmpEvent(t, expected, ev, "")
@@ -258,13 +269,20 @@ func TestCreateAndDeleteResource(t *testing.T) {
258269
subClient, subID := subscribeToAllEvents(t, ctx, c, correlationID)
259270
const switchID = "1"
260271

272+
isDiscoveryResourceBatchObservable := test.IsDiscoveryResourceBatchObservable(ctx, t, deviceID)
273+
261274
for i := 0; i < 5; i++ {
262275
fmt.Printf("iteration %v\n", i)
263276
createSwitchResource(t, ctx, c, deviceID, switchID)
264277
expectedCreateEvents := createSwitchResourceExpectedEvents(t, deviceID, subID, correlationID, switchID)
265-
validateEvents(t, subClient, expectedCreateEvents, true)
278+
validateEvents(t, subClient, expectedCreateEvents)
266279
deleteSwitchResource(t, ctx, c, deviceID, switchID)
267280
expectedDeleteEvents := deleteSwitchResourceExpectedEvents(t, deviceID, subID, correlationID, switchID)
268-
validateEvents(t, subClient, expectedDeleteEvents, false)
281+
validateEvents(t, subClient, expectedDeleteEvents)
282+
283+
if isDiscoveryResourceBatchObservable {
284+
expectedEvents := batchDeleteSwitchChangedResourceExpected(t, deviceID, subID, correlationID, switchID)
285+
validateEvents(t, subClient, expectedEvents)
286+
}
269287
}
270288
}

test/test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,29 @@ func FindDeviceByName(ctx context.Context, name string) (deviceID string, _ erro
484484
return id, nil
485485
}
486486

487+
func IsDiscoveryResourceBatchObservable(ctx context.Context, t *testing.T, deviceID string) bool {
488+
devClient, err := NewSDKClient()
489+
require.NoError(t, err)
490+
defer func() {
491+
_ = devClient.Close(ctx)
492+
}()
493+
494+
device, links, err := devClient.GetRefDevice(ctx, deviceID)
495+
require.NoError(t, err)
496+
err = device.Release(ctx)
497+
require.NoError(t, err)
498+
links = links.GetResourceLinks(resources.ResourceType)
499+
if len(links) == 0 {
500+
return false
501+
}
502+
for _, iface := range links[0].Interfaces {
503+
if iface == interfaces.OC_IF_B && links[0].Policy.BitMask.Has(schema.Observable) {
504+
return true
505+
}
506+
}
507+
return false
508+
}
509+
487510
func CheckResource(ctx context.Context, t *testing.T, deviceID, href, resourceType string, checkFn func(schema.ResourceLink) bool) bool {
488511
devClient, err := NewSDKClient()
489512
require.NoError(t, err)

0 commit comments

Comments
 (0)