public void UpdateData_should_return_fail_without_retries_if_cant_read_data(ZooKeeperStatus errorStatus) { const string path = "zk/default"; zooKeeperClient.GetDataAsync(Arg.Any <GetDataRequest>()).Returns(GetDataResult.Unsuccessful(errorStatus, path, null)); zooKeeperClient.UpdateData(new UpdateDataRequest(path, dummyUpdate)) .Should() .BeEquivalentTo(UpdateDataResult.Unsuccessful(errorStatus, path, null)); zooKeeperClient.ReceivedWithAnyArgs(1) .GetDataAsync(Arg.Any <GetDataRequest>()); zooKeeperClient.DidNotReceiveWithAnyArgs() .SetDataAsync(Arg.Any <SetDataRequest>()); }
public void UpdateData_should_return_false_after_one_attempt_if_set_data_fails(ZooKeeperStatus failFastStatus) { const string path = "zk/default"; var bytes = new byte[] { 0, 1, 1, 2, 3, 5, 8, 13 }; zooKeeperClient.GetDataAsync(Arg.Any <GetDataRequest>()) .Returns(GetDataResult.Successful(path, bytes, Stat)); zooKeeperClient.SetDataAsync(Arg.Any <SetDataRequest>()) .Returns(SetDataResult.Unsuccessful(failFastStatus, path, null)); zooKeeperClient.UpdateData(new UpdateDataRequest(path, dummyUpdate)) .Should() .BeEquivalentTo(UpdateDataResult.Unsuccessful(failFastStatus, path, null)); zooKeeperClient.ReceivedWithAnyArgs(1).SetDataAsync(Arg.Any <SetDataRequest>()); }
/// <summary> /// <para>Trying to update node data with optimistic concurrency strategy according to given <paramref name="request"/>.</para> /// <para>Check returned <see cref="UpdateDataResult"/> to see if operation was successful.</para> /// </summary> public static async Task <UpdateDataResult> UpdateDataAsync(this IZooKeeperClient zooKeeperClient, UpdateDataRequest request) { try { for (var i = 0; i < request.Attempts; i++) { var readResult = await zooKeeperClient.GetDataAsync(new GetDataRequest(request.Path)).ConfigureAwait(false); if (!readResult.IsSuccessful) { return(UpdateDataResult.Unsuccessful(readResult.Status, readResult.Path, readResult.Exception)); } var newData = request.Update(readResult.Data); if (ByteArrayKey.Equals(readResult.Data, newData)) { return(UpdateDataResult.Successful(readResult.Path)); } var setDataRequest = new SetDataRequest(request.Path, newData) { Version = readResult.Stat.Version }; var updateResult = await zooKeeperClient.SetDataAsync(setDataRequest).ConfigureAwait(false); if (updateResult.Status == ZooKeeperStatus.VersionsMismatch) { continue; } return(updateResult.IsSuccessful ? UpdateDataResult.Successful(updateResult.Path) : UpdateDataResult.Unsuccessful(updateResult.Status, updateResult.Path, updateResult.Exception)); } return(UpdateDataResult.Unsuccessful(ZooKeeperStatus.VersionsMismatch, request.Path, null)); } catch (Exception e) { return(UpdateDataResult.Unsuccessful(ZooKeeperStatus.UnknownError, request.Path, e)); } }
public void UpdateData_should_make_all_attempts_to_set_data_and_return_fail() { const string path = "zk/default"; const int attempts = 10; var bytes = new byte[] { 0, 1, 1, 2, 3, 5, 8, 13 }; zooKeeperClient.GetDataAsync(Arg.Any <GetDataRequest>()) .Returns(GetDataResult.Successful(path, bytes, Stat)); zooKeeperClient.SetDataAsync(Arg.Any <SetDataRequest>()) .Returns(SetDataResult.Unsuccessful(ZooKeeperStatus.VersionsMismatch, path, null)); zooKeeperClient.UpdateData(new UpdateDataRequest(path, dummyUpdate) { Attempts = attempts }) .Should() .BeEquivalentTo(UpdateDataResult.Unsuccessful(ZooKeeperStatus.VersionsMismatch, path, null)); zooKeeperClient.ReceivedWithAnyArgs(attempts) .SetDataAsync(Arg.Any <SetDataRequest>()); }