@@ -15,11 +15,9 @@ namespace Smdn.Net.MuninNode.Protocol;
1515#pragma warning disable IDE0040
1616partial class MuninProtocolHandlerTests {
1717#pragma warning restore IDE0040
18- // TODO: Plugin
19- // TODO: AggregatePluginProvider
2018 private class TransactionCallbackPluginProvider : IPluginProvider , ITransactionCallback {
2119 public IReadOnlyCollection < IPlugin > Plugins => Array . Empty < IPlugin > ( ) ;
22- [ Obsolete ] public INodeSessionCallback ? SessionCallback => null ;
20+ [ Obsolete ] public INodeSessionCallback ? SessionCallback => null ;
2321
2422 public Action < CancellationToken > ? OnStartTransaction { get ; init ; }
2523 public Action < CancellationToken > ? OnEndTransaction { get ; init ; }
@@ -105,6 +103,76 @@ public void HandleTransactionEndAsync_ITransactionCallback_IPluginProvider(Cance
105103 Assert . That ( numberOfOnEndTransactionInvoked , Is . EqualTo ( 1 ) ) ;
106104 }
107105
106+ [ Test ]
107+ [ CancelAfter ( 1000 ) ]
108+ public void HandleTransactionStartAsync_ITransactionCallback_AggregatePluginProvider ( CancellationToken cancellationToken )
109+ {
110+ const string HostName = "munin-node.localhost" ;
111+
112+ var numberOfOnStartTransactionInvoked = 0 ;
113+ var numberOfOnEndTransactionInvoked = 0 ;
114+ var handler = new MuninProtocolHandler (
115+ profile : new MuninNodeProfile ( ) {
116+ HostName = HostName ,
117+ PluginProvider = new AggregatePluginProvider ( [
118+ new TransactionCallbackPluginProvider ( ) {
119+ OnStartTransaction = ct => {
120+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
121+ numberOfOnStartTransactionInvoked ++ ;
122+ } ,
123+ OnEndTransaction = ct => {
124+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
125+ numberOfOnEndTransactionInvoked ++ ;
126+ }
127+ }
128+ ] )
129+ }
130+ ) ;
131+ var client = new PseudoMuninNodeClient ( ) ;
132+
133+ Assert . That (
134+ async ( ) => await handler . HandleTransactionStartAsync ( client , cancellationToken ) ,
135+ Throws . Nothing
136+ ) ;
137+ Assert . That ( numberOfOnStartTransactionInvoked , Is . EqualTo ( 1 ) ) ;
138+ Assert . That ( numberOfOnEndTransactionInvoked , Is . Zero ) ;
139+ }
140+
141+ [ Test ]
142+ [ CancelAfter ( 1000 ) ]
143+ public void HandleTransactionEndAsync_ITransactionCallback_AggregatePluginProvider ( CancellationToken cancellationToken )
144+ {
145+ const string HostName = "munin-node.localhost" ;
146+
147+ var numberOfOnStartTransactionInvoked = 0 ;
148+ var numberOfOnEndTransactionInvoked = 0 ;
149+ var handler = new MuninProtocolHandler (
150+ profile : new MuninNodeProfile ( ) {
151+ HostName = HostName ,
152+ PluginProvider = new AggregatePluginProvider ( [
153+ new TransactionCallbackPluginProvider ( ) {
154+ OnStartTransaction = ct => {
155+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
156+ numberOfOnStartTransactionInvoked ++ ;
157+ } ,
158+ OnEndTransaction = ct => {
159+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
160+ numberOfOnEndTransactionInvoked ++ ;
161+ }
162+ }
163+ ] )
164+ }
165+ ) ;
166+ var client = new PseudoMuninNodeClient ( ) ;
167+
168+ Assert . That (
169+ async ( ) => await handler . HandleTransactionEndAsync ( client , cancellationToken ) ,
170+ Throws . Nothing
171+ ) ;
172+ Assert . That ( numberOfOnStartTransactionInvoked , Is . Zero ) ;
173+ Assert . That ( numberOfOnEndTransactionInvoked , Is . EqualTo ( 1 ) ) ;
174+ }
175+
108176 private class TransactionCallbackPlugin ( string name ) : IPlugin , ITransactionCallback {
109177 public string Name => name ;
110178 public IPluginGraphAttributes GraphAttributes => throw new NotImplementedException ( ) ;
@@ -351,4 +419,108 @@ CancellationToken cancellationToken
351419 Assert . That ( numberOfPluginOnStartTransactionInvoked , Is . Zero ) ;
352420 Assert . That ( numberOfPluginOnEndTransactionInvoked , multigraph ? Is . Zero : Is . EqualTo ( 1 ) ) ;
353421 }
422+
423+ private class TransactionCallbackExtendedPlugin : Plugin {
424+ public Action < CancellationToken > ? OnStartTransaction { get ; init ; }
425+ public Action < CancellationToken > ? OnEndTransaction { get ; init ; }
426+
427+ public TransactionCallbackExtendedPlugin ( )
428+ : base (
429+ name : "plugin" ,
430+ graphAttributes : new PluginGraphAttributes (
431+ title : "title" ,
432+ category : "test" ,
433+ verticalLabel : "test" ,
434+ scale : false ,
435+ arguments : "--args"
436+ ) ,
437+ fields : Array . Empty < IPluginField > ( )
438+ )
439+ {
440+ }
441+
442+ protected override ValueTask StartTransactionAsync ( CancellationToken cancellationToken )
443+ {
444+ OnStartTransaction ? . Invoke ( cancellationToken ) ;
445+
446+ return default ;
447+ }
448+
449+ protected override ValueTask EndTransactionAsync ( CancellationToken cancellationToken )
450+ {
451+ OnEndTransaction ? . Invoke ( cancellationToken ) ;
452+
453+ return default ;
454+ }
455+ }
456+
457+ [ Test ]
458+ [ CancelAfter ( 1000 ) ]
459+ public void HandleTransactionStartAsync_ITransactionCallback_Plugin ( CancellationToken cancellationToken )
460+ {
461+ const string HostName = "munin-node.localhost" ;
462+
463+ var numberOfOnStartTransactionInvoked = 0 ;
464+ var numberOfOnEndTransactionInvoked = 0 ;
465+ var handler = new MuninProtocolHandler (
466+ profile : new MuninNodeProfile ( ) {
467+ HostName = HostName ,
468+ PluginProvider = new PluginProvider ( [
469+ new TransactionCallbackExtendedPlugin ( ) {
470+ OnStartTransaction = ct => {
471+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
472+ numberOfOnStartTransactionInvoked ++ ;
473+ } ,
474+ OnEndTransaction = ct => {
475+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
476+ numberOfOnEndTransactionInvoked ++ ;
477+ }
478+ }
479+ ] )
480+ }
481+ ) ;
482+ var client = new PseudoMuninNodeClient ( ) ;
483+
484+ Assert . That (
485+ async ( ) => await handler . HandleTransactionStartAsync ( client , cancellationToken ) ,
486+ Throws . Nothing
487+ ) ;
488+ Assert . That ( numberOfOnStartTransactionInvoked , Is . EqualTo ( 1 ) ) ;
489+ Assert . That ( numberOfOnEndTransactionInvoked , Is . Zero ) ;
490+ }
491+
492+ [ Test ]
493+ [ CancelAfter ( 1000 ) ]
494+ public void HandleTransactionEndAsync_ITransactionCallback_Plugin ( CancellationToken cancellationToken )
495+ {
496+ const string HostName = "munin-node.localhost" ;
497+
498+ var numberOfOnStartTransactionInvoked = 0 ;
499+ var numberOfOnEndTransactionInvoked = 0 ;
500+ var handler = new MuninProtocolHandler (
501+ profile : new MuninNodeProfile ( ) {
502+ HostName = HostName ,
503+ PluginProvider = new PluginProvider ( [
504+ new TransactionCallbackExtendedPlugin ( ) {
505+ OnStartTransaction = ct => {
506+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
507+ numberOfOnStartTransactionInvoked ++ ;
508+ } ,
509+ OnEndTransaction = ct => {
510+ Assert . That ( ct , Is . EqualTo ( cancellationToken ) ) ;
511+ numberOfOnEndTransactionInvoked ++ ;
512+ }
513+ }
514+ ] )
515+ }
516+ ) ;
517+ var client = new PseudoMuninNodeClient ( ) ;
518+
519+ Assert . That (
520+ async ( ) => await handler . HandleTransactionEndAsync ( client , cancellationToken ) ,
521+ Throws . Nothing
522+ ) ;
523+ Assert . That ( numberOfOnStartTransactionInvoked , Is . Zero ) ;
524+ Assert . That ( numberOfOnEndTransactionInvoked , Is . EqualTo ( 1 ) ) ;
525+ }
354526}
0 commit comments