コード例 #1
0
        public void TestAddAndRemove()
        {
            var state = new InMemoryAssetStatusState() as IAssetStatusState;

            Assert.NotNull(state);

            {
                var count = state.GetSubscriptions().Result.Count;
                Assert.True(count == 0, $"Expected a zero count with a new state, got ${count} instead");
            }

            const string clientId1 = "307CA37C-2422-4AD9-A512-34DFB3BC7507";
            const string clientId2 = "B0CCDF38-AA7E-4BCB-8692-53C898D6A469";

            var model1 = new AssetUpdateSubscriptionModel()
            {
                ProjectUid = Guid.Parse("2A131E56-880E-44E8-901F-86D3CD3CBDBB")
            };
            var model2 = new AssetUpdateSubscriptionModel()
            {
                ProjectUid = Guid.Parse("D5C5A0FA-2480-4ACB-BDCE-0CDF10998BF8")
            };
            var model3 = new AssetUpdateSubscriptionModel()
            {
                ProjectUid = Guid.Parse("BDE14152-1B1B-4809-85B2-384DA1D91B43")
            };

            // Add the first model
            state.AddSubscription(clientId1, model1);

            // count should be 1
            {
                var items = state.GetSubscriptions().Result;
                Assert.True(items.Count == 1, $"Added a model, count should be one but it is ${items.Count}");
                Assert.Equal(model1, items[0]);
            }

            // Add the second model, with the same identifier (this should replace the existing one)
            state.AddSubscription(clientId1, model2);

            // count should be 1
            {
                var items = state.GetSubscriptions().Result;
                Assert.True(items.Count == 1, $"Added a model with the same identifier, count should be one but it is ${items.Count}");
                Assert.Equal(model2, items[0]);
            }

            // Add another model, with another identifier
            state.AddSubscription(clientId2, model3);

            // Count should be 2
            {
                var items = state.GetSubscriptions().Result;
                Assert.True(items.Count == 2, $"Added a model with a new identifier, count should be two but it is ${items.Count}");
                Assert.Contains(model2, items);
                Assert.Contains(model3, items);
            }

            // Remove the first subscription and an unknown subscription
            state.RemoveSubscription(clientId1);
            state.RemoveSubscription("does not exist id");

            // Count should be 1
            {
                var items = state.GetSubscriptions().Result;
                Assert.True(items.Count == 1, $"Removed a subscription, count should be one but it is ${items.Count}");
                Assert.Contains(model3, items);
            }

            state.RemoveSubscription(clientId2);

            // should be empty
            {
                var count = state.GetSubscriptions().Result.Count;
                Assert.True(count == 0, $"Expected a zero count with all clients removed, got ${count} instead");
            }
        }