1
namespace Org
.Lwes
.Tests
5 using System
.Threading
;
7 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
11 using Org
.Lwes
.Emitter
;
12 using Org
.Lwes
.Listener
;
15 public class EventListenerTests
20 public void AccessDefaultListener()
24 EventName
= "UserLogin",
27 UserName
= new { Name = "username", Token = TypeToken.STRING, Value = "bob" }
,
28 Password
= new { Name = "password", Token = TypeToken.UINT64, Value = 0xfeedabbadeadbeefUL }
,
29 ClientIP
= new { Name = "clientIP", Token = TypeToken.IP_ADDR, Value = IPAddress.Parse("127.0.0.1") }
,
30 Successful
= new { Name = "successful", Token = TypeToken.BOOLEAN, Value = false }
34 Event receivedEvent
= default(Event
);
36 // Mock an IEventSink that records the incomming event...
37 Mock
<IEventSink
> mock
= new Mock
<IEventSink
>();
38 mock
.SetupGet(sink
=> sink
.IsThreadSafe
).Returns(true);
39 mock
.Setup(framework
=> framework
.HandleEventArrival(It
.IsAny
<IEventSinkRegistrationKey
>(), It
.IsAny
<Event
>()))
40 .Callback
<IEventSinkRegistrationKey
, Event
>((k
, ev
) =>
47 using (IEventListener listener
= EventListener
.CreateDefault())
49 Assert
.IsNotNull(listener
);
50 listener
.RegisterEventSink(mock
.Object
).Activate();
52 IEventEmitter emitter
= EventEmitter
.CreateDefault();
53 Assert
.IsNotNull(emitter
);
55 // Create the event...
56 sourceEvent
= new Event(e
.EventName
)
57 .SetValue(e
.Attributes
.UserName
.Name
, e
.Attributes
.UserName
.Value
)
58 .SetValue(e
.Attributes
.Password
.Name
, e
.Attributes
.Password
.Value
)
59 .SetValue(e
.Attributes
.ClientIP
.Name
, e
.Attributes
.ClientIP
.Value
)
60 .SetValue(e
.Attributes
.Successful
.Name
, e
.Attributes
.Successful
.Value
);
62 long time_out_ticks
= DateTime
.Now
.Ticks
+ TimeSpan
.FromSeconds(20).Ticks
;
64 while (!done
&& DateTime
.Now
.Ticks
< time_out_ticks
)
66 emitter
.Emit(sourceEvent
);
70 Assert
.IsTrue(done
, "Should have received an event");
71 Assert
.AreEqual(sourceEvent
[e
.Attributes
.UserName
.Name
].GetValue
<string>(), receivedEvent
[e
.Attributes
.UserName
.Name
].GetValue
<string>());
72 Assert
.AreEqual(sourceEvent
[e
.Attributes
.Password
.Name
].GetValue
<ulong>(), receivedEvent
[e
.Attributes
.Password
.Name
].GetValue
<ulong>());
73 Assert
.AreEqual(sourceEvent
[e
.Attributes
.ClientIP
.Name
].GetValue
<IPAddress
>(), receivedEvent
[e
.Attributes
.ClientIP
.Name
].GetValue
<IPAddress
>());
74 Assert
.AreEqual(sourceEvent
[e
.Attributes
.Successful
.Name
].GetValue
<bool>(), receivedEvent
[e
.Attributes
.Successful
.Name
].GetValue
<bool>());
75 Console
.Write(receivedEvent
.ToString());