Пример #1
0
        public async Task ExtendedTest1()
        {
            // Get your key from https://console.developers.google.com/apis/credentials
            var apiKey = "AIzaSyCtcFQMgRIUHhSuXggm4BtXT4eZvUrBWN0";
            // See https://docs.google.com/spreadsheets/d/1KBamVmgEUX-fyogMJ48TT6h2kAMKyWU1uBL5skCGRBM
            var sheetId           = "1KBamVmgEUX-fyogMJ48TT6h2kAMKyWU1uBL5skCGRBM";
            var sheetName         = "MySheet1"; // Has to match the sheet name
            var googleSheetsStore = new GoogleSheetsKeyValueStore(new InMemoryKeyValueStore(), apiKey, sheetId, sheetName);


            var localStore = new InMemoryKeyValueStore();
            var testStore  = new FeatureFlagStore(localStore, googleSheetsStore);

            IoC.inject.SetSingleton <FeatureFlagManager <FeatureFlag> >(new FeatureFlagManager <FeatureFlag>(testStore));

            var key1 = "MyFlag1";
            var key2 = "MyFlag2";
            var key3 = "MyFlag3";

            Assert.NotNull(await googleSheetsStore.Get <FeatureFlag>(key1, null));
            Assert.NotNull(await googleSheetsStore.Get <FeatureFlag>(key2, null));
            Assert.NotNull(await testStore.Get(key2, null));
            Assert.NotNull(await FeatureFlagManager <FeatureFlag> .instance.GetFeatureFlag(key2));

            Assert.False(await FeatureFlag.IsEnabled(key1));
            Assert.True(await FeatureFlag.IsEnabled(key2));

            var flag3_1 = await FeatureFlagManager <FeatureFlag> .instance.GetFeatureFlag(key3);

            Assert.Equal(40, flag3_1.rolloutPercentage);

            var localState3_1 = await localStore.Get <IFeatureFlagLocalState>(key3, null);

            var percent3_1 = localState3_1.randomPercentage;

            Assert.NotEqual(0, percent3_1);
            if (percent3_1 < flag3_1.rolloutPercentage)
            {
                Assert.True(await FeatureFlag.IsEnabled(key3));
            }
            else
            {
                Assert.False(await FeatureFlag.IsEnabled(key3));
            }
            localState3_1.randomPercentage = flag3_1.rolloutPercentage - 1;
            await localStore.Set(key3, localState3_1);

            var localState3_2 = await localStore.Get <IFeatureFlagLocalState>(key3, null);

            Assert.NotEqual(percent3_1, localState3_2.randomPercentage);

            var flag3_2 = await FeatureFlagManager <FeatureFlag> .instance.GetFeatureFlag(key3);

            Assert.Equal(localState3_2.randomPercentage, flag3_2.localState.randomPercentage);
            Assert.True(await flag3_2.IsEnabled());
            Assert.True(await FeatureFlag.IsEnabled(key3));
        }
Пример #2
0
        public async Task TestGoogleSheetsKeyValueStore()
        {
            // Get your key from https://console.developers.google.com/apis/credentials
            var apiKey = "AIzaSyCtcFQMgRIUHhSuXggm4BtXT4eZvUrBWN0";
            // See https://docs.google.com/spreadsheets/d/13R9y6lnUMgRPC0PinJ23tACC6Flgogxa7h7SVaaLhT0
            var sheetId   = "13R9y6lnUMgRPC0PinJ23tACC6Flgogxa7h7SVaaLhT0";
            var sheetName = "UpdateEntriesV1"; // Has to match the sheet name

            var refreshDelayInMs = 1000;
            // The cache is where the data from the sheet will be locally stored in:
            var cache = new InMemoryKeyValueStore(); // Could also persist to disc here
            var store = new GoogleSheetsKeyValueStore(cache, apiKey, sheetId, sheetName, refreshDelayInMs);

            var download1 = store.dowloadOnlineDataDebounced();
            var download2 = store.dowloadOnlineDataDebounced();
            var download3 = store.dowloadOnlineDataDebounced();

            // After the refresh delay redownload was allowed again:
            await TaskV2.Delay(refreshDelayInMs * 3);

            Assert.True(await download1); // first trigger downloaded the data
            Assert.NotEmpty(store.latestRawSheetData);
            // Triggering it instant a second time will not download the data again:
            Assert.False(await download2); // Second trigger was skipped
            Assert.True(await download3);

            var entry1 = await store.Get <MySheetEntry>("1", null);

            Assert.NotNull(entry1);
            Assert.Equal("a", entry1.myString1);
            Assert.Equal(new List <int>()
            {
                1, 2, 3, 4
            }, entry1.myArray1);
            Assert.Equal("b", entry1.myObj1.a);
            Assert.Equal(5, entry1.myInt1);
            Assert.Equal(1.4, entry1.myDouble1);

            var entry2 = await store.Get <MySheetEntry>("2", null);

            Assert.NotNull(entry2);
            Assert.Null(entry2.myString1);
            Assert.Empty(entry2.myArray1);
            Assert.Null(entry2.myObj1);
            Assert.Equal(0, entry2.myInt1);
            Assert.Equal(0, entry2.myDouble1);
        }