1+ using System ;
2+ using System . Threading ;
3+ using System . Threading . Tasks ;
4+ using NServiceBus ;
5+ using NServiceBus . AcceptanceTesting ;
6+ using NServiceBus . Features ;
7+ using NUnit . Framework ;
8+ using Conventions = NServiceBus . AcceptanceTesting . Customization . Conventions ;
9+
10+ public class DoNotDeliverBefore : BridgeAcceptanceTest
11+ {
12+ static string OriginalEndpointName = Conventions . EndpointNamingConvention ( typeof ( OriginalEndpoint ) ) ;
13+
14+ [ Test ]
15+ public async Task Should_set_TTBR_correctly ( )
16+ {
17+ var ctx = await Scenario . Define < Context > ( )
18+ . WithBridge ( ( bridgeConfiguration , transportBeingTested ) =>
19+ {
20+ transportBeingTested . HasEndpoint ( "_" ) ;
21+ bridgeConfiguration . AddTransport ( transportBeingTested ) ;
22+
23+ bridgeConfiguration . AddTestTransportEndpoint ( new BridgeEndpoint ( OriginalEndpointName ) ) ;
24+ } )
25+ . WithEndpoint < MigratedEndpoint > ( )
26+ . WithEndpoint < OriginalEndpoint > ( endpoint => endpoint . When ( ( session , context ) =>
27+ {
28+ context . SendStartTime = DateTimeOffset . UtcNow ;
29+ return Task . CompletedTask ;
30+ } ) )
31+ . Done ( c => c . SendStartTime != DateTimeOffset . MinValue && DateTimeOffset . UtcNow >= c . SendStartTime . AddSeconds ( 10 ) )
32+ . Run ( ) ;
33+
34+ Assert . That ( ctx . NumberOfMessagesReceived , Is . EqualTo ( 1 ) ) ;
35+ }
36+
37+ public class Context : BridgeScenarioContext
38+ {
39+ public int NumberOfMessagesReceived { get ; set ; }
40+ public DateTimeOffset SendStartTime { get ; set ; }
41+ }
42+
43+ public class MigratedEndpoint : EndpointConfigurationBuilder
44+ {
45+ public MigratedEndpoint ( )
46+ {
47+ CustomEndpointName ( OriginalEndpointName ) ;
48+ EndpointSetup < DefaultTestServer > ( c =>
49+ {
50+ // Set concurrency to 1 to ensure that the first message
51+ // will delay sufficiently long for the TTBR setting
52+ // to expire on the second message
53+ c . LimitMessageProcessingConcurrencyTo ( 1 ) ;
54+ c . PurgeOnStartup ( true ) ;
55+ } ) ;
56+ }
57+
58+ public class AMessageHandler ( Context testContext ) : IHandleMessages < AMessage >
59+ {
60+ public async Task Handle ( AMessage message , IMessageHandlerContext context )
61+ {
62+ testContext . NumberOfMessagesReceived ++ ;
63+
64+ await Task . Delay ( 5000 ) ;
65+ }
66+ }
67+ }
68+
69+ public class OriginalEndpoint : EndpointConfigurationBuilder
70+ {
71+ public OriginalEndpoint ( ) => EndpointSetup < DefaultServer > ( cfg => cfg . EnableFeature < SendLocalFeature > ( ) ) ;
72+
73+ public class SendLocalFeature : Feature
74+ {
75+ protected override void Setup ( FeatureConfigurationContext context ) => context . RegisterStartupTask ( ( ) => new SendLocalStartupTask ( ) ) ;
76+
77+ class SendLocalStartupTask : FeatureStartupTask
78+ {
79+ protected override async Task OnStart ( IMessageSession session , CancellationToken cancellationToken = default )
80+ {
81+ await session . SendLocal ( new AMessage ( ) , cancellationToken ) ;
82+ await session . SendLocal ( new AMessage ( ) , cancellationToken ) ;
83+ await Task . Delay ( TimeSpan . FromSeconds ( 5 ) , cancellationToken ) ;
84+ }
85+
86+ protected override Task OnStop ( IMessageSession session , CancellationToken cancellationToken = default ) => Task . CompletedTask ;
87+ }
88+ }
89+ }
90+
91+ [ TimeToBeReceived ( "00:00:03" ) ]
92+ public class AMessage : IMessage ;
93+ }
0 commit comments