Skip to content

Commit e3e3321

Browse files
[.NET] Add interfaces for state store client types to enable mocking the client (#1001)
#991
1 parent 85dd712 commit e3e3321

File tree

23 files changed

+142
-104
lines changed

23 files changed

+142
-104
lines changed

dotnet/samples/Services/LeasedLockClient/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ await leasedLockClient.AcquireLockAsync(
6161
Console.WriteLine("Successfully acquired lock. Now altering a shared resource in the State Store");
6262

6363
string newValue = Guid.NewGuid().ToString();
64-
StateStoreSetResponse setResponse = await stateStoreClient.SetAsync(
64+
IStateStoreSetResponse setResponse = await stateStoreClient.SetAsync(
6565
_sharedResourceKey,
6666
newValue,
6767
new StateStoreSetRequestOptions()

dotnet/samples/Services/PassiveReplication/PassiveReplicationNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private async Task DoLeaderThingsAsync(CancellationToken cancellationToken)
142142
// if the fencing token becomes out-of-date. For example, this thread may stall for a day, wake up,
143143
// and try to alter a shared resource. By that point, another node will have acquired the lock and
144144
// a newer fencing token will have been created.
145-
StateStoreSetResponse setResponse = await _stateStoreClient.SetAsync(
145+
IStateStoreSetResponse setResponse = await _stateStoreClient.SetAsync(
146146
_sharedResourceKeyToUpdate,
147147
new StateStoreValue(Guid.NewGuid().ToString()),
148148
new StateStoreSetRequestOptions()

dotnet/samples/Services/StateStoreClient/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
PersistEntry = true
3131
};
3232

33-
StateStoreSetResponse setResponse =
33+
IStateStoreSetResponse setResponse =
3434
await stateStoreClient.SetAsync(stateStoreKey, stateStoreValue, setOptions);
3535

3636
if (setResponse.Success)
@@ -42,7 +42,7 @@
4242
Console.WriteLine($"Failed to set key {stateStoreKey} with value {stateStoreValue}");
4343
}
4444

45-
StateStoreGetResponse getResponse = await stateStoreClient.GetAsync(stateStoreKey!);
45+
IStateStoreGetResponse getResponse = await stateStoreClient.GetAsync(stateStoreKey!);
4646

4747
if (getResponse.Value != null)
4848
{
@@ -53,7 +53,7 @@
5353
Console.WriteLine($"The key {stateStoreKey} is not currently in the state store");
5454
}
5555

56-
StateStoreDeleteResponse deleteResponse = await stateStoreClient.DeleteAsync(stateStoreKey!);
56+
IStateStoreDeleteResponse deleteResponse = await stateStoreClient.DeleteAsync(stateStoreKey!);
5757

5858
if (deleteResponse.DeletedItemsCount == 1)
5959
{

dotnet/samples/applications/EventDrivenApp/InputWorker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private async Task ProcessSensorData(MqttSessionClient sessionClient, Cancellati
6565
try
6666
{
6767
// Fetch the historical sensor data from the state store
68-
StateStoreGetResponse response = await stateStoreClient.GetAsync(Constants.StateStoreSensorKey, null, cancellationToken);
68+
IStateStoreGetResponse response = await stateStoreClient.GetAsync(Constants.StateStoreSensorKey, null, cancellationToken);
6969
if (response.Value != null)
7070
{
7171
data = JsonSerializer.Deserialize<List<SensorData>>(response.Value.GetString()) ?? [];

dotnet/samples/applications/EventDrivenApp/OutputWorker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private async Task ProcessWindow(MqttSessionClient sessionClient, CancellationTo
5151
await using StateStoreClient stateStoreClient = new(applicationContext, sessionClient);
5252
{
5353
// Fetch the past sensor data from the state store
54-
StateStoreGetResponse response = await stateStoreClient.GetAsync(Constants.StateStoreSensorKey);
54+
IStateStoreGetResponse response = await stateStoreClient.GetAsync(Constants.StateStoreSensorKey);
5555
if (response.Value == null)
5656
{
5757
await Console.Out.WriteLineAsync("Sensor data not found in state store");

dotnet/src/Azure.Iot.Operations.Connector/AdrClientWrapper.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public AdrClientWrapper(IAdrServiceClient adrServiceClient, IAssetFileMonitor? a
4343
_monitor.AssetFileChanged += AssetFileChanged;
4444
}
4545

46-
/// </inheritdoc>
46+
/// <inheritdoc/>
4747
public void ObserveDevices()
4848
{
4949
// Any pre-existing devices will trigger the monitor's callback which triggers the ADR client to observe updates
5050
_monitor.ObserveDevices();
5151
}
5252

53-
/// </inheritdoc>
53+
/// <inheritdoc/>
5454
public async Task UnobserveDevicesAsync(CancellationToken cancellationToken = default)
5555
{
5656
_monitor.UnobserveDevices();
@@ -64,14 +64,14 @@ public async Task UnobserveDevicesAsync(CancellationToken cancellationToken = de
6464
_observedDevices.Clear();
6565
}
6666

67-
/// </inheritdoc>
67+
/// <inheritdoc/>
6868
public void ObserveAssets(string deviceName, string inboundEndpointName)
6969
{
7070
// Any pre-existing assets will trigger the monitor's callback which triggers the ADR client to observe updates
7171
_monitor.ObserveAssets(deviceName, inboundEndpointName);
7272
}
7373

74-
/// </inheritdoc>
74+
/// <inheritdoc/>
7575
public async Task UnobserveAssetsAsync(string deviceName, string inboundEndpointName, CancellationToken cancellationToken = default)
7676
{
7777
_monitor.UnobserveAssets(deviceName, inboundEndpointName);
@@ -88,7 +88,7 @@ public async Task UnobserveAssetsAsync(string deviceName, string inboundEndpoint
8888
}
8989
}
9090

91-
/// </inheritdoc>
91+
/// <inheritdoc/>
9292
public async Task UnobserveAllAsync(CancellationToken cancellationToken = default)
9393
{
9494
_monitor.UnobserveAll();
@@ -113,13 +113,13 @@ public async Task UnobserveAllAsync(CancellationToken cancellationToken = defaul
113113
_observedDevices.Clear();
114114
}
115115

116-
/// </inheritdoc>
116+
/// <inheritdoc/>
117117
public EndpointCredentials GetEndpointCredentials(string deviceName, string inboundEndpointName, InboundEndpointSchemaMapValue inboundEndpoint)
118118
{
119119
return _monitor.GetEndpointCredentials(deviceName, inboundEndpointName, inboundEndpoint);
120120
}
121121

122-
/// </inheritdoc>
122+
/// <inheritdoc/>
123123
public async Task<DeviceStatus> UpdateDeviceStatusAsync(
124124
string deviceName,
125125
string inboundEndpointName,
@@ -130,7 +130,7 @@ public async Task<DeviceStatus> UpdateDeviceStatusAsync(
130130
return await _client.UpdateDeviceStatusAsync(deviceName, inboundEndpointName, status, commandTimeout, cancellationToken);
131131
}
132132

133-
/// </inheritdoc>
133+
/// <inheritdoc/>
134134
public async Task<AssetStatus> UpdateAssetStatusAsync(
135135
string deviceName,
136136
string inboundEndpointName,
@@ -141,37 +141,37 @@ public async Task<AssetStatus> UpdateAssetStatusAsync(
141141
return await _client.UpdateAssetStatusAsync(deviceName, inboundEndpointName, request, commandTimeout, cancellationToken);
142142
}
143143

144-
/// </inheritdoc>
144+
/// <inheritdoc/>
145145
public IEnumerable<string> GetAssetNames(string deviceName, string inboundEndpointName)
146146
{
147147
return _monitor.GetAssetNames(deviceName, inboundEndpointName);
148148
}
149149

150-
/// </inheritdoc>
150+
/// <inheritdoc/>
151151
public IEnumerable<string> GetInboundEndpointNames(string deviceName)
152152
{
153153
return _monitor.GetInboundEndpointNames(deviceName);
154154
}
155155

156-
/// </inheritdoc>
156+
/// <inheritdoc/>
157157
public IEnumerable<string> GetDeviceNames()
158158
{
159159
return _monitor.GetDeviceNames();
160160
}
161161

162-
/// </inheritdoc>
162+
/// <inheritdoc/>
163163
public Task<CreateOrUpdateDiscoveredAssetResponsePayload> CreateOrUpdateDiscoveredAssetAsync(string deviceName, string inboundEndpointName, CreateOrUpdateDiscoveredAssetRequest request, TimeSpan? commandTimeout = null, CancellationToken cancellationToken = default)
164164
{
165165
return _client.CreateOrUpdateDiscoveredAssetAsync(deviceName, inboundEndpointName, request, commandTimeout, cancellationToken);
166166
}
167167

168-
/// </inheritdoc>
168+
/// <inheritdoc/>
169169
public Task<CreateOrUpdateDiscoveredDeviceResponsePayload> CreateOrUpdateDiscoveredDeviceAsync(CreateOrUpdateDiscoveredDeviceRequestSchema request, string inboundEndpointType, TimeSpan? commandTimeout = null, CancellationToken cancellationToken = default)
170170
{
171171
return _client.CreateOrUpdateDiscoveredDeviceAsync(request, inboundEndpointType, commandTimeout, cancellationToken);
172172
}
173173

174-
/// </inheritdoc>
174+
/// <inheritdoc/>
175175
public ValueTask DisposeAsync()
176176
{
177177
return _client.DisposeAsync();

dotnet/src/Azure.Iot.Operations.Connector/ConnectorWorker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public async Task ForwardSampledDatasetAsync(string deviceName, string inboundEn
335335

336336
string stateStoreKey = destination.Configuration.Key ?? throw new AssetConfigurationException("Cannot publish sampled dataset to state store as it has no configured key");
337337

338-
StateStoreSetResponse response = await stateStoreClient.SetAsync(stateStoreKey, new(serializedPayload));
338+
IStateStoreSetResponse response = await stateStoreClient.SetAsync(stateStoreKey, new(serializedPayload));
339339

340340
if (response.Success)
341341
{

dotnet/src/Azure.Iot.Operations.Connector/Files/AssetFileMonitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public class AssetFileMonitor : IAssetFileMonitor
3535

3636
private readonly IFilesMonitorFactory _filesMonitorFactory;
3737

38-
/// </inheritdoc>
38+
/// <inheritdoc/>
3939
public event EventHandler<AssetFileChangedEventArgs>? AssetFileChanged;
4040

41-
/// </inheritdoc>
41+
/// <inheritdoc/>
4242
public event EventHandler<DeviceFileChangedEventArgs>? DeviceFileChanged;
4343

4444
/// <summary>

dotnet/src/Azure.Iot.Operations.Services/LeasedLock/LeasedLockClient.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private async Task<AcquireLockResponse> TryAcquireLockWithoutEnablingAutoRenewal
217217
? new StateStoreValue(LockHolderName)
218218
: new StateStoreValue(string.Format(ValueFormat, LockHolderName, options.SessionId));
219219
Debug.Assert(_lockKey != null);
220-
StateStoreSetResponse setResponse =
220+
IStateStoreSetResponse setResponse =
221221
await _stateStoreClient.SetAsync(
222222
_lockKey,
223223
value,
@@ -413,7 +413,7 @@ public async Task AcquireLockAndUpdateValueAsync(StateStoreKey key, Func<StateSt
413413

414414
try
415415
{
416-
StateStoreGetResponse getResponse = await _stateStoreClient.GetAsync(key, timeoutPerRequest, cancellationToken: cancellationToken);
416+
IStateStoreGetResponse getResponse = await _stateStoreClient.GetAsync(key, timeoutPerRequest, cancellationToken: cancellationToken);
417417

418418
StateStoreValue? newValue = updateValueFunc.Invoke(getResponse.Value);
419419

@@ -424,7 +424,7 @@ public async Task AcquireLockAndUpdateValueAsync(StateStoreKey key, Func<StateSt
424424
FencingToken = acquireLockResponse.FencingToken,
425425
};
426426

427-
StateStoreDeleteResponse deleteResponse = await _stateStoreClient.DeleteAsync(key, deleteOptions, timeoutPerRequest, cancellationToken);
427+
IStateStoreDeleteResponse deleteResponse = await _stateStoreClient.DeleteAsync(key, deleteOptions, timeoutPerRequest, cancellationToken);
428428
valueChanged = deleteResponse.DeletedItemsCount == 1;
429429
}
430430
else
@@ -434,7 +434,7 @@ public async Task AcquireLockAndUpdateValueAsync(StateStoreKey key, Func<StateSt
434434
FencingToken = acquireLockResponse.FencingToken,
435435
};
436436

437-
StateStoreSetResponse setResponse = await _stateStoreClient.SetAsync(key, newValue, setOptions, timeoutPerRequest, cancellationToken: cancellationToken);
437+
IStateStoreSetResponse setResponse = await _stateStoreClient.SetAsync(key, newValue, setOptions, timeoutPerRequest, cancellationToken: cancellationToken);
438438
valueChanged = setResponse.Success;
439439
}
440440
}
@@ -469,7 +469,7 @@ public virtual async Task<GetLockHolderResponse> GetLockHolderAsync(TimeSpan? ti
469469
ObjectDisposedException.ThrowIf(_disposed, this);
470470

471471
Debug.Assert(_lockKey != null);
472-
StateStoreGetResponse getResponse = await _stateStoreClient.GetAsync(
472+
IStateStoreGetResponse getResponse = await _stateStoreClient.GetAsync(
473473
_lockKey,
474474
timeout,
475475
cancellationToken).ConfigureAwait(false);
@@ -500,7 +500,7 @@ public virtual async Task<ReleaseLockResponse> ReleaseLockAsync(ReleaseLockReque
500500
? new StateStoreValue(LockHolderName)
501501
: new StateStoreValue(string.Format(ValueFormat, LockHolderName, options.SessionId));
502502
Debug.Assert(_lockKey != null);
503-
StateStoreDeleteResponse deleteResponse =
503+
IStateStoreDeleteResponse deleteResponse =
504504
await _stateStoreClient.DeleteAsync(
505505
_lockKey,
506506
new StateStoreDeleteRequestOptions()

dotnet/src/Azure.Iot.Operations.Services/StateStore/IStateStoreClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IStateStoreClient : IAsyncDisposable
2323
/// <param name="requestTimeout">The optional timeout for this request.</param>
2424
/// <param name="cancellationToken">Cancellation token.</param>
2525
/// <returns>The service response containing the current value of the key in the State Store.</returns>
26-
Task<StateStoreGetResponse> GetAsync(StateStoreKey key, TimeSpan? requestTimeout = null, CancellationToken cancellationToken = default);
26+
Task<IStateStoreGetResponse> GetAsync(StateStoreKey key, TimeSpan? requestTimeout = null, CancellationToken cancellationToken = default);
2727

2828
/// <summary>
2929
/// Set the value of a key in the State Store.
@@ -34,7 +34,7 @@ public interface IStateStoreClient : IAsyncDisposable
3434
/// <param name="requestTimeout">The optional timeout for this request.</param>
3535
/// <param name="cancellationToken">Cancellation token.</param>
3636
/// <returns>The service response detailing if the operation succeeded and (optionally) the previous value of this key.</returns>
37-
Task<StateStoreSetResponse> SetAsync(StateStoreKey key, StateStoreValue value, StateStoreSetRequestOptions? options = null, TimeSpan? requestTimeout = null, CancellationToken cancellationToken = default);
37+
Task<IStateStoreSetResponse> SetAsync(StateStoreKey key, StateStoreValue value, StateStoreSetRequestOptions? options = null, TimeSpan? requestTimeout = null, CancellationToken cancellationToken = default);
3838

3939
/// <summary>
4040
/// Delete the provided key from the State Store.
@@ -44,7 +44,7 @@ public interface IStateStoreClient : IAsyncDisposable
4444
/// <param name="requestTimeout">The request timeout.</param>
4545
/// <param name="cancellationToken">Cancellation token.</param>
4646
/// <returns>The details of the service response.</returns>
47-
Task<StateStoreDeleteResponse> DeleteAsync(StateStoreKey key, StateStoreDeleteRequestOptions? options = null, TimeSpan? requestTimeout = null, CancellationToken cancellationToken = default);
47+
Task<IStateStoreDeleteResponse> DeleteAsync(StateStoreKey key, StateStoreDeleteRequestOptions? options = null, TimeSpan? requestTimeout = null, CancellationToken cancellationToken = default);
4848

4949
/// <summary>
5050
/// Begin receiving events each time the provided key is updated, deleted, or created. Events will be delivered

0 commit comments

Comments
 (0)