Beispiel #1
0
        private static async Task GenerateUsers(IHDictionary <string, User> users)
        {
            await users.GetOrAddAsync("Rod", new User("Rod", 19, true));

            await users.GetOrAddAsync("Jane", new User("Jane", 20, true));

            await users.GetOrAddAsync("Freddy", new User("Freddy", 23, true));
        }
        private async Task SetTask(IHDictionary <string, string> dictionary, SemaphoreSlim started, int id)
        {
            var i = 0;

            while (!_stop)
            {
                i++;
                // put new value and update last state
                // note: the value in the map/Near Cache is *always* larger or equal to valuePut
                // assertion: valueMap >= valuePut
                await dictionary.SetAsync(Key, i.ToString()).CAF();

                _valuePut = i;

                // ensure we have a value
                if (i == 1)
                {
                    started.Release();
                }

                // check if we see our last update
                var valueStr = await dictionary.GetAsync(Key).CAF();

                if (valueStr == null)
                {
                    continue;                   // ignore nulls (?)
                }
                var valueInt = int.Parse(valueStr);
                if (valueInt == i)
                {
                    continue;                // match = ok
                }
                _assertionViolationCount++;
                Logger.LogWarning($"Err: set {i} but got {valueInt}.");

                // maybe it needs a bit of time?
                await Task.Delay(200).CAF();

                // test again and stop if really lost
                valueStr = await dictionary.GetAsync(Key).CAF();

                valueInt = int.Parse(valueStr);
                if (valueInt == i)
                {
                    continue;                // fixed
                }
                Logger.LogWarning($"Err: still getting {valueInt} instead of {i}.");

                // maybe it needs a lot of time?
                // in non-strict, we'll give it plenty

                // abort all
                _stop = true;
            }
            Logger.LogInformation($"AddOrUpdate task {id} performed {i} operations.");
        }
        public async Task TearDown()
        {
            if (_dictionary != null)
            {
                await _dictionary.DestroyAsync();
            }
            _dictionary = null;

            if (_client != null)
            {
                await _client.DisposeAsync();
            }
            _client = null;
        }
        public async Task Init()
        {
            _client = await CreateAndStartClientAsync();

            var client = _client as HazelcastClient;

            Assert.That(client, Is.Not.Null);
            SerializationService = client.SerializationService;

            _dictionary = await _client.GetDictionaryAsync <object, object>("nc-" + TestUtils.RandomString());

            var nearCache = GetNearCache(_dictionary);

            Assert.That(nearCache, Is.InstanceOf <NearCaching.NearCache>());
        }
        private async Task RunTestInternal(IHDictionary <string, string> dictionary)
        {
            var tasks = new List <Task>();

            const int readerTaskCount = 4;

            // start 1 writer thread
            using var started = new SemaphoreSlim(0);
            tasks.Add(Task.Run(() => SetTask(dictionary, started, 0)));

            // wait for writer thread to start before starting getter threads
            var hasStarted = await started.WaitAsync(20_000);

            if (!hasStarted)
            {
                Assert.Fail("Fail to start AddOrUpdate task.");
            }

            // start reader tasks
            for (var i = 0; i < readerTaskCount; i++)
            {
                var id = i;
                tasks.Add(Task.Run(() => GetTask(dictionary, id)));
            }

            // stop after maxRuntime seconds
            var j = 0;

            while (!_stop && j++ < MaxRuntime)
            {
                await Task.Delay(1000);
            }

            if (!_stop)
            {
                Logger.LogInformation("Good: problem did not occur within " + MaxRuntime + "s.");
            }

            _stop = true;
            await Task.WhenAll(tasks);
        }
        private async Task GetTask(IHDictionary <string, string> dictionary, int id)
        {
            var n = 0;

            while (!_stop)
            {
                n++;

                // get the value
                var valueStr = await dictionary.GetAsync(Key).CAF();

                // that should never happen!
                Assert.That(valueStr, Is.Not.Null);

                // parse the value (to get some CPU load)
                var valueInt = int.Parse(valueStr);
                Assert.AreEqual(valueInt.ToString(), valueStr);

                // slow down
                await Task.Delay(100).CAF();
            }
            Logger.LogInformation($"Get task {id} performed {n} operations.");
        }
        public async Task Setup()
        {
            _dictionary = await Client.GetDictionaryAsync <object, object>(CreateUniqueName());

            _ = await _dictionary.GetAsync(new object());
        }
Beispiel #8
0
 public async Task SetUp()
 {
     _dictionary = await Client.GetDictionaryAsync <string, HazelcastJsonValue>(CreateUniqueName());
 }
 internal NearCacheBase GetNearCache <TKey, TValue>(IHDictionary <TKey, TValue> dictionary)
 {
     return(dictionary is HDictionaryWithCache <TKey, TValue> cachedDictionary ? cachedDictionary.NearCache.InnerCache : null);
 }