Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Hazelcast.Net.Tests/Remote/ClientMapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1407,5 +1408,42 @@ public async Task TestValuesPredicate()
Assert.IsTrue(enumerator.MoveNext());
Assert.AreEqual("value1", enumerator.Current);
}

[Test]
public async Task TestTTLUpdated()
{
var map = await Client.GetMapAsync<string, string>(CreateUniqueName());
// in seconds
var maxTTL = 100;
var minTTL = 3;
var key = "key1";
var latch = new ManualResetEvent(false);

var starTime = Stopwatch.StartNew();
await map.SetAsync(key, "value1", TimeSpan.FromSeconds(maxTTL));

// Notify when key removed
var parallelCheck = Task.Run(async () =>
{
await AssertEx.SucceedsEventually(
async () => Assert.False(await map.ContainsKeyAsync(key)),
maxTTL * 1000,
500);
latch.Set();
});

// Reduce the TTL
Assert.True(await map.UpdateTimeToLive(key, TimeSpan.FromSeconds(minTTL)));

await latch.WaitOneAsync();
starTime.Stop();
var elapsed = starTime.ElapsedMilliseconds;

// Definitively, less then maxTTL
Assert.Less(elapsed, maxTTL * 1000);

// Close to min TTL
Assert.Less(elapsed, minTTL * 1000 * 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not sure if this will always work if the server is working slow, etc.
Does the ContainsKeyAsync trigger eviction check on expiry, if so, it may be safe but if we do it on scheduled basis on the server side, there may be a time diff until that scheduler is triggered and entry is evicted. If that is the case, this check may fail depending on the scheduler interval.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it may not work well in slow cases. Do you have any suggestion?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove that check :) By the way you normally do not need to introduce your own latch but we have assertTrueEventually kind of test utilities which would simply the test. Just check Java side. By the way, did we cover all the existing tests at the Java client for this API? If not, we can also add missing ones.

}
}
}
10 changes: 8 additions & 2 deletions src/Hazelcast.Net/DistributedObjects/Impl/HMap.Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,15 @@ Task SetTransientAsync(IData keyData, IData valueData, TimeSpan timeToLive, Time
#endif
}

public Task<bool> UpdateTimeToLive(TKey key, TimeSpan timeToLive)
public async Task<bool> UpdateTimeToLive(TKey key, TimeSpan timeToLive)
{
throw new NotImplementedException();
var keyData = ToSafeData(key);
var seconds = Convert.ToInt64(timeToLive.TotalSeconds);
var requestMessage = MapSetTtlCodec.EncodeRequest(Name, keyData, seconds);

var result= await Cluster.Messaging.SendToKeyPartitionOwnerAsync(requestMessage, keyData).CfAwait();
var decoded = MapSetTtlCodec.DecodeResponse(result);
return decoded.Response;
}
}
}
Loading