Skip to content

Commit 5e6fd73

Browse files
committed
Refactor StreamUpdates method to be asynchronous
1 parent 692d427 commit 5e6fd73

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

src/BinanceBot.Market/MarketDepthManager.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,24 @@ public async Task BuildAsync(MarketDepth marketDepth, short orderBookDepth = 10,
172172
/// Stream <see cref="MarketDepth"/> updates
173173
/// </summary>
174174
/// <param name="marketDepth">Market depth</param>
175+
/// Stream <see cref="MarketDepth"/> updates asynchronously.
176+
/// </summary>
177+
/// <param name="marketDepth">Market depth</param>
175178
/// <param name="updateInterval">Update interval (100ms or 1000ms)</param>
176-
public void StreamUpdates(MarketDepth marketDepth, TimeSpan? updateInterval = default, CancellationToken ct = default)
179+
public async Task StreamUpdatesAsync(MarketDepth marketDepth, TimeSpan? updateInterval = default, CancellationToken ct = default)
177180
{
178181
if (marketDepth == null)
179182
throw new ArgumentNullException(nameof(marketDepth));
180183

181184
// Step 1 & 2: Open WebSocket and buffer events
182-
_subscription = _webSocketClient.SpotStreams.SubscribeToOrderBookUpdatesAsync(
185+
var subscriptionResult = await _webSocketClient.SpotStreams.SubscribeToOrderBookUpdatesAsync(
183186
marketDepth.Symbol,
184187
updateInterval.HasValue ? (int)updateInterval.Value.TotalMilliseconds : (int)_defaultUpdateInterval.TotalMilliseconds,
185188
data => OnDepthUpdate(marketDepth, data),
186-
ct)
187-
.Result.Data;
188-
}
189+
ct);
189190

190-
/// <summary>
191+
_subscription = subscriptionResult.Data;
192+
}
191193
/// Stop streaming updates and unsubscribe
192194
/// </summary>
193195
public async Task StopStreamingAsync(CancellationToken ct = default)

src/BinanceBot.Market/MarketMakerBot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public override async Task RunAsync()
140140
var marketDepthManager = new MarketDepthManager(_binanceRestClient, _webSocketClient, Logger);
141141
var updateInterval = TimeSpan.FromMilliseconds(1000);
142142
// stream order book updates
143-
marketDepthManager.StreamUpdates(_marketDepth, updateInterval);
143+
await marketDepthManager.StreamUpdatesAsync(_marketDepth, updateInterval);
144144
// build order book
145145
await marketDepthManager.BuildAsync(_marketDepth, orderBookDepth: 100, updateInterval: updateInterval);
146146
}

tests/BinanceBot.Market.Tests/MarketDepthManagerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() =>
114114
}
115115

116116
[Fact]
117-
public void StreamUpdates_WithNullMarketDepth_ThrowsArgumentNullException()
117+
public async Task StreamUpdates_WithNullMarketDepth_ThrowsArgumentNullException()
118118
{
119119
// Arrange
120120
var manager = new MarketDepthManager(_mockRestClient.Object, _mockSocketClient.Object, _mockLogger.Object);
121121

122122
// Act & Assert
123-
Assert.Throws<ArgumentNullException>(() =>
124-
manager.StreamUpdates(null));
123+
await Assert.ThrowsAsync<ArgumentNullException>(() =>
124+
manager.StreamUpdatesAsync(null));
125125
}
126126

127127
[Fact]

0 commit comments

Comments
 (0)