1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// AgentBaseTests.cs
3
3
4
+ using System . Collections . Concurrent ;
4
5
using FluentAssertions ;
5
6
using Google . Protobuf . Reflection ;
6
7
using Microsoft . AutoGen . Abstractions ;
8
+ using Microsoft . Extensions . DependencyInjection ;
9
+ using Microsoft . Extensions . Hosting ;
7
10
using Microsoft . Extensions . Logging ;
8
11
using Moq ;
9
12
using Xunit ;
13
+ using static Microsoft . AutoGen . Agents . Tests . AgentBaseTests ;
10
14
11
15
namespace Microsoft . AutoGen . Agents . Tests ;
12
16
13
- public class AgentBaseTests
17
+ [ Collection ( ClusterFixtureCollection . Name ) ]
18
+ public class AgentBaseTests ( InMemoryAgentRuntimeFixture fixture )
14
19
{
20
+ private readonly InMemoryAgentRuntimeFixture _fixture = fixture ;
21
+
15
22
[ Fact ]
16
23
public async Task ItInvokeRightHandlerTestAsync ( )
17
24
{
@@ -26,12 +33,36 @@ public async Task ItInvokeRightHandlerTestAsync()
26
33
agent . ReceivedItems [ 1 ] . Should ( ) . Be ( 42 ) ;
27
34
}
28
35
36
+ [ Fact ]
37
+ public async Task ItDelegateMessageToTestAgentAsync ( )
38
+ {
39
+ var client = _fixture . AppHost . Services . GetRequiredService < Client > ( ) ;
40
+
41
+ await client . PublishMessageAsync ( new TextMessage ( )
42
+ {
43
+ Source = nameof ( ItDelegateMessageToTestAgentAsync ) ,
44
+ TextMessage_ = "buffer"
45
+ } , token : CancellationToken . None ) ;
46
+
47
+ // wait for 10 seconds
48
+ var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
49
+ while ( ! TestAgent . ReceivedMessages . ContainsKey ( nameof ( ItDelegateMessageToTestAgentAsync ) ) && ! cts . Token . IsCancellationRequested )
50
+ {
51
+ await Task . Delay ( 100 ) ;
52
+ }
53
+
54
+ TestAgent . ReceivedMessages [ nameof ( ItDelegateMessageToTestAgentAsync ) ] . Should ( ) . NotBeNull ( ) ;
55
+ }
56
+
29
57
/// <summary>
30
58
/// The test agent is a simple agent that is used for testing purposes.
31
59
/// </summary>
32
- public class TestAgent : AgentBase , IHandle < string > , IHandle < int >
60
+ public class TestAgent : AgentBase , IHandle < string > , IHandle < int > , IHandle < TextMessage >
33
61
{
34
- public TestAgent ( IAgentRuntime context , EventTypes eventTypes , Logger < AgentBase > logger ) : base ( context , eventTypes , logger )
62
+ public TestAgent (
63
+ IAgentRuntime context ,
64
+ [ FromKeyedServices ( "EventTypes" ) ] EventTypes eventTypes ,
65
+ Logger < AgentBase > ? logger = null ) : base ( context , eventTypes , logger )
35
66
{
36
67
}
37
68
@@ -47,6 +78,49 @@ public Task Handle(int item)
47
78
return Task . CompletedTask ;
48
79
}
49
80
81
+ public Task Handle ( TextMessage item )
82
+ {
83
+ ReceivedMessages [ item . Source ] = item . TextMessage_ ;
84
+ return Task . CompletedTask ;
85
+ }
86
+
50
87
public List < object > ReceivedItems { get ; private set ; } = [ ] ;
88
+
89
+ /// <summary>
90
+ /// Key: source
91
+ /// Value: message
92
+ /// </summary>
93
+ public static ConcurrentDictionary < string , object > ReceivedMessages { get ; private set ; } = new ( ) ;
94
+ }
95
+ }
96
+
97
+ public sealed class InMemoryAgentRuntimeFixture : IDisposable
98
+ {
99
+ public InMemoryAgentRuntimeFixture ( )
100
+ {
101
+ var builder = Microsoft . Extensions . Hosting . Host . CreateApplicationBuilder ( ) ;
102
+
103
+ // step 1: create in-memory agent runtime
104
+ // step 2: register TestAgent to that agent runtime
105
+ builder
106
+ . AddAgentService ( local : true , useGrpc : false )
107
+ . AddAgentWorker ( local : true )
108
+ . AddAgent < TestAgent > ( nameof ( TestAgent ) ) ;
109
+
110
+ AppHost = builder . Build ( ) ;
111
+ AppHost . StartAsync ( ) . Wait ( ) ;
112
+ }
113
+ public IHost AppHost { get ; }
114
+
115
+ void IDisposable . Dispose ( )
116
+ {
117
+ AppHost . StopAsync ( ) . Wait ( ) ;
118
+ AppHost . Dispose ( ) ;
51
119
}
52
120
}
121
+
122
+ [ CollectionDefinition ( Name ) ]
123
+ public sealed class ClusterFixtureCollection : ICollectionFixture < InMemoryAgentRuntimeFixture >
124
+ {
125
+ public const string Name = nameof ( ClusterFixtureCollection ) ;
126
+ }
0 commit comments