public async Task CreateAndDeleteConnectionStatusSubscription()

in DeviceBridgeTests/Services/ConnectionStatusSubscriptionServiceTests.cs [40:72]


        public async Task CreateAndDeleteConnectionStatusSubscription()
        {
            using (ShimsContext.Create())
            {
                _storageProviderMock.Invocations.Clear();

                // Check create.
                var testSub = TestUtils.GetTestSubscription("test-device-id", DeviceSubscriptionType.ConnectionStatus);
                _storageProviderMock.Setup(p => p.CreateOrUpdateDeviceSubscription(It.IsAny<Logger>(), "test-device-id", DeviceSubscriptionType.ConnectionStatus, "http://abc", It.IsAny<CancellationToken>())).Returns(Task.FromResult(testSub));
                var subscriptionService = new ConnectionStatusSubscriptionService(LogManager.GetCurrentClassLogger(), _connectionManagerMock.Object, _storageProviderMock.Object, _subscriptionCallbackFactoryMock.Object);
                SemaphoreSlim createSemaphore = null;
                TestUtils.CaptureSemaphoreOnWait(capturedSemaphore => createSemaphore = capturedSemaphore);
                var result = await subscriptionService.CreateOrUpdateConnectionStatusSubscription(LogManager.GetCurrentClassLogger(), "test-device-id", "http://abc", default);
                Assert.AreEqual(testSub, result);
                _connectionManagerMock.Verify(p => p.SetConnectionStatusCallback("test-device-id", It.IsAny<Func<ConnectionStatus, ConnectionStatusChangeReason, Task>>()), Times.Once);

                // Check delete.
                SemaphoreSlim deleteSemaphore = null;
                TestUtils.CaptureSemaphoreOnWait(capturedSemaphore => deleteSemaphore = capturedSemaphore);
                await subscriptionService.DeleteConnectionStatusSubscription(LogManager.GetCurrentClassLogger(), "test-device-id", default);
                _storageProviderMock.Verify(p => p.DeleteDeviceSubscription(It.IsAny<Logger>(), "test-device-id", DeviceSubscriptionType.ConnectionStatus, default));
                _connectionManagerMock.Verify(p => p.RemoveConnectionStatusCallback("test-device-id"), Times.Once);

                // Check that create and delete lock on the same mutex.
                Assert.AreEqual(createSemaphore, deleteSemaphore);

                // Check that operation in a different device Id locks on a different mutex.
                SemaphoreSlim anotherDeviceSemaphore = null;
                TestUtils.CaptureSemaphoreOnWait(capturedSemaphore => anotherDeviceSemaphore = capturedSemaphore);
                await subscriptionService.DeleteConnectionStatusSubscription(LogManager.GetCurrentClassLogger(), "another-device-id", default);
                Assert.AreNotEqual(deleteSemaphore, anotherDeviceSemaphore);
            }
        }