@@ -2015,39 +2015,38 @@ public async Task CanPrune2()
2015
2015
}
2016
2016
}
2017
2017
2018
- [ FactWithTimeout ]
2019
- public async Task CanUseWebSockets ( )
2018
+ [ TheoryWithTimeout ]
2019
+ [ InlineData ( false ) ]
2020
+ [ InlineData ( true ) ]
2021
+ public async Task CanUseWebSockets ( bool legacyAPI )
2020
2022
{
2021
2023
using ( var tester = ServerTester . Create ( ) )
2022
2024
{
2023
2025
tester . Client . WaitServerStarted ( ) ;
2024
2026
var key = new BitcoinExtKey ( new ExtKey ( ) , tester . Network ) ;
2025
2027
var pubkey = tester . CreateDerivationStrategy ( key . Neuter ( ) , true ) ;
2026
2028
await tester . Client . TrackAsync ( pubkey ) ;
2027
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2029
+ using ( var connected = CreateNotificationSession ( tester , legacyAPI ) )
2028
2030
{
2029
- connected . ListenNewBlock ( ) ;
2031
+ var legacy = connected as WebsocketNotificationSessionLegacy ;
2032
+ legacy ? . ListenNewBlock ( ) ;
2030
2033
var expectedBlockId = tester . Explorer . CreateRPCClient ( ) . Generate ( 1 ) [ 0 ] ;
2031
- var blockEvent = ( Models . NewBlockEvent ) connected . NextEvent ( Cancel ) ;
2032
- // Sometimes Postgres backend emit one more block during warmup. That's not a bug,
2033
- // but make test flaky.
2034
- if ( blockEvent . Hash != expectedBlockId )
2035
- blockEvent = ( Models . NewBlockEvent ) connected . NextEvent ( Cancel ) ;
2034
+ var blockEvent = await WaitBlock ( connected , expectedBlockId , Cancel ) ;
2036
2035
2037
2036
Assert . True ( blockEvent . EventId != 0 ) ;
2038
2037
Assert . Equal ( expectedBlockId , blockEvent . Hash ) ;
2039
2038
Assert . NotEqual ( 0 , blockEvent . Height ) ;
2040
2039
2041
2040
Assert . Equal ( 1 , blockEvent . Confirmations ) ;
2042
2041
2043
- connected . ListenDerivationSchemes ( new [ ] { pubkey } ) ;
2042
+ legacy ? . ListenDerivationSchemes ( new [ ] { pubkey } ) ;
2044
2043
await tester . SendToAddressAsync ( tester . AddressOf ( pubkey , "0/1" ) , Money . Coins ( 1.0m ) ) ;
2045
2044
2046
2045
var txEvent = ( Models . NewTransactionEvent ) connected . NextEvent ( Cancel ) ;
2047
2046
Assert . Equal ( txEvent . DerivationStrategy , pubkey ) ;
2048
2047
}
2049
2048
2050
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2049
+ using ( var connected = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) )
2051
2050
{
2052
2051
connected . ListenAllDerivationSchemes ( ) ;
2053
2052
await tester . SendToAddressAsync ( tester . AddressOf ( pubkey , "0/1" ) , Money . Coins ( 1.0m ) ) ;
@@ -2056,7 +2055,7 @@ public async Task CanUseWebSockets()
2056
2055
Assert . Equal ( txEvent . DerivationStrategy , pubkey ) ;
2057
2056
}
2058
2057
2059
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2058
+ using ( var connected = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) )
2060
2059
{
2061
2060
connected . ListenAllTrackedSource ( ) ;
2062
2061
await tester . SendToAddressAsync ( tester . AddressOf ( pubkey , "0/1" ) , Money . Coins ( 1.0m ) ) ;
@@ -2123,7 +2122,7 @@ public async Task CanUseWebSockets2()
2123
2122
2124
2123
( var pubkey , var pubkey2 ) = ( wLegacy . DerivationScheme , wSegwit . DerivationScheme ) ;
2125
2124
2126
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2125
+ using ( var connected = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) )
2127
2126
{
2128
2127
connected . ListenAllDerivationSchemes ( ) ;
2129
2128
tester . Explorer . CreateRPCClient ( ) . SendCommand ( RPCOperations . sendmany , "" ,
@@ -2313,7 +2312,7 @@ public async Task CanTrack3()
2313
2312
var key = new BitcoinExtKey ( new ExtKey ( ) , tester . Network ) ;
2314
2313
var pubkey = tester . CreateDerivationStrategy ( key . Neuter ( ) ) ;
2315
2314
await tester . Client . TrackAsync ( pubkey ) ;
2316
- var events = tester . Client . CreateWebsocketNotificationSession ( ) ;
2315
+ var events = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) ;
2317
2316
events . ListenDerivationSchemes ( new [ ] { pubkey } ) ;
2318
2317
2319
2318
Logs . Tester . LogInformation ( "Let's send to 0/0, 0/1, 0/2, 0, 1" ) ;
@@ -2418,35 +2417,37 @@ public async Task CanTrackSeveralTransactions()
2418
2417
}
2419
2418
}
2420
2419
2421
- [ FactWithTimeout ]
2422
- public async void CanUseWebSocketsOnAddress ( )
2420
+ [ TheoryWithTimeout ]
2421
+ [ InlineData ( false ) ]
2422
+ [ InlineData ( true ) ]
2423
+ public async Task CanUseWebSocketsOnAddress ( bool legacyAPI )
2423
2424
{
2424
2425
using ( var tester = ServerTester . Create ( ) )
2425
2426
{
2426
2427
tester . Client . WaitServerStarted ( ) ;
2427
- await Task . Delay ( 500 ) ;
2428
2428
var key = new Key ( ) ;
2429
2429
var pubkey = TrackedSource . Create ( key . PubKey . GetAddress ( ScriptPubKeyType . Legacy , tester . Network ) ) ;
2430
2430
tester . Client . Track ( pubkey ) ;
2431
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2431
+ using ( var connected = CreateNotificationSession ( tester , legacyAPI ) )
2432
2432
{
2433
- connected . ListenNewBlock ( ) ;
2434
- var expectedBlockId = tester . Explorer . CreateRPCClient ( ) . Generate ( 1 ) [ 0 ] ;
2435
- var blockEvent = ( Models . NewBlockEvent ) connected . NextEvent ( Cancel ) ;
2433
+ var legacy = connected as WebsocketNotificationSessionLegacy ;
2434
+ legacy ? . ListenNewBlock ( ) ;
2435
+ var expectedBlockId = ( await tester . Explorer . CreateRPCClient ( ) . GenerateAsync ( 1 ) ) [ 0 ] ;
2436
+ var blockEvent = await WaitBlock ( connected , expectedBlockId , Cancel ) ;
2436
2437
Assert . Equal ( expectedBlockId , blockEvent . Hash ) ;
2437
2438
Assert . NotEqual ( 0 , blockEvent . Height ) ;
2438
2439
2439
- connected . ListenTrackedSources ( new [ ] { pubkey } ) ;
2440
+ legacy ? . ListenTrackedSources ( new [ ] { pubkey } ) ;
2440
2441
tester . SendToAddress ( pubkey . Address , Money . Coins ( 1.0m ) ) ;
2441
2442
2442
- var txEvent = ( Models . NewTransactionEvent ) connected . NextEvent ( Cancel ) ;
2443
+ var txEvent = ( Models . NewTransactionEvent ) await connected . NextEventAsync ( Cancel ) ;
2443
2444
Assert . NotEmpty ( txEvent . Outputs ) ;
2444
2445
Assert . Equal ( pubkey . Address . ScriptPubKey , txEvent . Outputs [ 0 ] . ScriptPubKey ) ;
2445
2446
Assert . Equal ( pubkey . Address , txEvent . Outputs [ 0 ] . Address ) ;
2446
2447
Assert . Equal ( txEvent . TrackedSource , pubkey ) ;
2447
2448
}
2448
2449
2449
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2450
+ using ( var connected = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) )
2450
2451
{
2451
2452
connected . ListenAllTrackedSource ( ) ;
2452
2453
tester . SendToAddress ( pubkey . Address , Money . Coins ( 1.0m ) ) ;
@@ -2457,6 +2458,19 @@ public async void CanUseWebSocketsOnAddress()
2457
2458
}
2458
2459
}
2459
2460
2461
+ private WebsocketNotificationSession CreateNotificationSession ( ServerTester tester , bool legacyAPI )
2462
+ => legacyAPI ? tester . Client . CreateWebsocketNotificationSessionLegacy ( ) : tester . Client . CreateWebsocketNotificationSession ( ) ;
2463
+
2464
+ private async Task < Models . NewBlockEvent > WaitBlock ( WebsocketNotificationSession connected , uint256 expectedBlockId , CancellationToken cancel )
2465
+ {
2466
+ var evt = await connected . NextEventAsync ( Cancel ) ;
2467
+ while ( evt is not Models . NewBlockEvent b || b . Hash != expectedBlockId )
2468
+ {
2469
+ evt = await connected . NextEventAsync ( Cancel ) ;
2470
+ }
2471
+ return ( Models . NewBlockEvent ) evt ;
2472
+ }
2473
+
2460
2474
[ FactWithTimeout ]
2461
2475
public async Task CanUseWebSocketsOnAddress2 ( )
2462
2476
{
@@ -2471,7 +2485,7 @@ public async Task CanUseWebSocketsOnAddress2()
2471
2485
2472
2486
await tester . Client . TrackAsync ( pubkey ) ;
2473
2487
await tester . Client . TrackAsync ( pubkey2 ) ;
2474
- using ( var connected = tester . Client . CreateWebsocketNotificationSession ( ) )
2488
+ using ( var connected = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) )
2475
2489
{
2476
2490
connected . ListenAllTrackedSource ( ) ;
2477
2491
tester . Explorer . CreateRPCClient ( ) . SendCommand ( RPCOperations . sendmany , "" ,
@@ -2920,7 +2934,7 @@ public async Task CanRescan()
2920
2934
var txId3 = tester . SendToAddress ( tester . AddressOf ( key , "0/0" ) , Money . Coins ( 1.0m ) ) ;
2921
2935
var txId4 = tester . SendToAddress ( tester . AddressOf ( key , "0/0" ) , Money . Coins ( 1.0m ) ) ;
2922
2936
var tx4 = tester . RPC . GetRawTransaction ( txId4 ) ;
2923
- var notify = tester . Client . CreateWebsocketNotificationSession ( ) ;
2937
+ var notify = tester . Client . CreateWebsocketNotificationSessionLegacy ( ) ;
2924
2938
notify . ListenNewBlock ( ) ;
2925
2939
var blockId = tester . RPC . Generate ( 1 ) [ 0 ] ;
2926
2940
var blockId2 = tester . RPC . Generate ( 1 ) [ 0 ] ;
@@ -3916,7 +3930,7 @@ public async Task ElementsTests()
3916
3930
DerivationFeature . Deposit ) . Address ) ;
3917
3931
3918
3932
Assert . IsType < BitcoinBlindedAddress > ( tester . Client . GetKeyInformation ( userDerivationScheme , address . ScriptPubKey ) . Address ) ;
3919
- using ( var session = await tester . Client . CreateWebsocketNotificationSessionAsync ( Timeout ) )
3933
+ using ( var session = await tester . Client . CreateWebsocketNotificationSessionLegacyAsync ( Timeout ) )
3920
3934
{
3921
3935
await session . ListenAllTrackedSourceAsync ( cancellation : Timeout ) ;
3922
3936
0 commit comments