private static async Task Fill <T>(IHMap <string, T> dictionary, Func <int, T> convert) { for (var i = 0; i < 10; i++) { var key = TestUtils.RandomString(); await dictionary.SetAsync(key, convert(i)); } }
private static async Task GenerateUsers(IHMap <string, User> users) { await users.PutIfAbsentAsync("Rod", new User("Rod", 19, true)); await users.PutIfAbsentAsync("Jane", new User("Jane", 20, true)); await users.PutIfAbsentAsync("Freddy", new User("Freddy", 23, true)); }
private async Task SetTask(IHMap <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()).CfAwait(); _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).CfAwait(); if (valueStr == null) { continue; // not found } 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).CfAwait(); // test again and stop if really lost valueStr = await dictionary.GetAsync(Key).CfAwait(); 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."); }
private async Task UseClient(IHMap <string, string> map, int durationMilliseconds, int pauseMilliseconds) { var count = durationMilliseconds / pauseMilliseconds; for (var i = 0; i < count; i++) { await UseClientOnce(map); await Task.Delay(pauseMilliseconds); } }
private static async Task RunTask(IHazelcastClient client, CancellationToken token, int id, int entryCount, ILogger logger) { logger.LogInformation($"Thread {id}: start"); IHMap <string, string> map = null; while (!token.IsCancellationRequested) { try { map = await client.GetMapAsync <string, string>("soak1").ConfigureAwait(false); break; } catch (Exception ex) { logger.LogError($"Thread {id} failed to acquire the map ({ex.GetType().Name}: {ex.Message}), abort"); return; } } logger.LogInformation($"Thread {id}: acquired the map"); while (!token.IsCancellationRequested) { try { var key = RandomProvider.Random.Next(0, entryCount).ToString(); var operation = RandomProvider.Random.Next(0, 100); if (operation < 30) { await map.GetAsync(key).ConfigureAwait(false); } else if (operation < 60) { await map.PutAsync(key, RandomProvider.Random.Next().ToString()).ConfigureAwait(false); } else if (operation < 80) { await map.GetValuesAsync(Predicates.Value().IsBetween(0, 10)).ConfigureAwait(false); } else { await map.ExecuteAsync(new UpdateEntryProcessor(key), key).ConfigureAwait(false); } _reports[id] += 1; } catch (Exception ex) { logger.LogError($"Thead {id} caught {ex.GetType().Name}: {ex.Message}"); } } }
public async Task TearDown() { if (_map != null) { await _map.DestroyAsync(); } _map = 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; _map = await _client.GetMapAsync <object, object>("nc-" + TestUtils.RandomString()); var nearCache = GetNearCache(_map); Assert.That(nearCache, Is.InstanceOf <NearCaching.NearCache>()); }
private async Task RunTestInternal(IHMap <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(IHMap <string, string> dictionary, int id) { var n = 0; while (!_stop) { n++; // get the value var valueStr = await dictionary.GetAsync(Key).CfAwait(); // 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).CfAwait(); } Logger.LogInformation($"Get task {id} performed {n} operations."); }
private async Task UseClientOnce(IHMap <string, string> map) { var stopwatch = Stopwatch.StartNew(); string key, value; try { key = value = RandomValue; stopwatch.Restart(); await map.SetAsync(key, value); HConsole.WriteLine(this, $"Set map value ({(int)stopwatch.Elapsed.TotalSeconds}s)"); } catch (TargetUnreachableException) { HConsole.WriteLine(this, "Failed to set map value: target unreachable"); } try { key = RandomValue; stopwatch.Restart(); value = await map.GetAsync(key); HConsole.WriteLine(this, $"Got map value ({(int)stopwatch.Elapsed.TotalSeconds}s)"); if (value != null) { Assert.That(value, Is.EqualTo(key)); } } catch (TargetUnreachableException) { HConsole.WriteLine(this, "Failed to get map value: target unreachable"); } }
public async Task SetUp() { _map = await Client.GetMapAsync <string, HazelcastJsonValue>(CreateUniqueName()); }
internal NearCacheBase GetNearCache <TKey, TValue>(IHMap <TKey, TValue> dictionary) { return(dictionary is HMapWithCache <TKey, TValue> cachedDictionary ? cachedDictionary.NearCache.InnerCache : null); }
public async Task Setup() { _map = await Client.GetMapAsync <object, object>(CreateUniqueName()); _ = await _map.GetAsync(new object()); }