DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
EventsTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Threading;
3 
4 using NUnit.Framework;
5 
6 namespace Deveel.Data.Diagnostics {
7  [TestFixture]
8  public class EventsTests : ContextBasedTest {
9  [Test]
10  public void AttachRouter() {
11  IEvent firedEvent = null;
12  Assert.DoesNotThrow(() => Query.Route<ErrorEvent>(@event => firedEvent = @event));
13  Assert.IsNull(firedEvent);
14  }
15 
16  [Test]
17  public void RouteError() {
18  var reset = new AutoResetEvent(false);
19 
20  IEvent firedEvent = null;
21  Query.Route<ErrorEvent>(e => {
22  firedEvent = e;
23  reset.Set();
24  });
25 
26  Query.OnError(new Exception("Test Error"));
27 
28  reset.WaitOne();
29 
30  Assert.IsNotNull(firedEvent);
31  Assert.IsInstanceOf<ErrorEvent>(firedEvent);
32  }
33 
34  [Test]
36  var reset = new AutoResetEvent(false);
37 
38  IEvent fired = null;
39  System.Context.Route<InformationEvent>(e => {
40  fired = e;
41  reset.Set();
42  });
43 
44  Query.OnVerbose("Test Message");
45 
46  reset.WaitOne(300);
47 
48  Assert.IsNotNull(fired);
49  Assert.IsInstanceOf<InformationEvent>(fired);
50 
51  var infoEvent = (InformationEvent) fired;
52  Assert.AreEqual(InformationLevel.Verbose, infoEvent.Level);
53  Assert.AreEqual("Test Message", infoEvent.Message);
54  }
55 
56  [Test]
57  public void RouteOnlyOnce() {
58  var reset1 = new AutoResetEvent(false);
59  var reset2 = new AutoResetEvent(false);
60 
61  IEvent systemFired = null;
62  System.Context.Route<InformationEvent>(e => {
63  systemFired = e;
64  reset1.Set();
65  });
66 
67  IEvent sessionFired = null;
68 
69  Session.Context.Route<ErrorEvent>(e => {
70  sessionFired = e;
71  reset2.Set();
72  });
73 
74  Query.OnVerbose("Test Message");
75 
76  reset1.WaitOne(300);
77  reset2.WaitOne(300);
78 
79  Assert.IsNotNull(systemFired);
80  Assert.IsNull(sessionFired);
81  }
82 
83  [Test]
85  var reset1 = new AutoResetEvent(false);
86  var reset2 = new AutoResetEvent(false);
87 
88  IEvent systemFired = null;
89  System.Context.Route<InformationEvent>(e => {
90  systemFired = e;
91  reset1.Set();
92  });
93 
94  IEvent sessionFired = null;
95 
96  Session.Context.Route<InformationEvent>(e => {
97  sessionFired = e;
98  reset2.Set();
99  }, e => e.Level == InformationLevel.Debug);
100 
101  Query.OnVerbose("Test Message");
102 
103  reset1.WaitOne(300);
104  reset2.WaitOne(300);
105 
106  Assert.IsNotNull(systemFired);
107  Assert.IsNull(sessionFired);
108  }
109 
110  [Test]
112  var reset1 = new AutoResetEvent(false);
113  var reset2 = new AutoResetEvent(false);
114 
115  IEvent systemFired = null;
116  System.Context.Route<InformationEvent>(e => {
117  systemFired = e;
118  reset1.Set();
119  });
120 
121  IEvent sessionFired = null;
122 
123  Session.Context.Route<InformationEvent>(e => {
124  sessionFired = e;
125  reset2.Set();
126  });
127 
128  Query.OnVerbose("Test Message");
129 
130  reset1.WaitOne(300);
131  reset2.WaitOne(300);
132 
133  Assert.IsNotNull(systemFired);
134  Assert.IsNotNull(sessionFired);
135  }
136 
137  [Test]
138  public void RouteOneRegisteredMany() {
139  var reset = new AutoResetEvent(false);
140 
141  QueryEvent a = null, b = null;
142  System.Context.Route<QueryEvent>(e => a = e);
143  System.Context.Route<QueryEvent>(e => b = e);
144 
145  IEvent fired = null;
146  System.Context.Route<InformationEvent>(e => {
147  fired = e;
148  reset.Set();
149  });
150 
151  Query.OnVerbose("Test Message");
152 
153  reset.WaitOne(300);
154 
155  Assert.IsNotNull(fired);
156  Assert.IsInstanceOf<InformationEvent>(fired);
157 
158  Assert.IsNull(a);
159  Assert.IsNull(b);
160 
161  var infoEvent = (InformationEvent) fired;
162  Assert.AreEqual(InformationLevel.Verbose, infoEvent.Level);
163  Assert.AreEqual("Test Message", infoEvent.Message);
164  }
165  }
166 }
This is an event occurred during the lifetime of a database.
Definition: IEvent.cs:24
This is a session that is constructed around a given user and a transaction, to the given database...
Definition: Session.cs:32
ISessionContext Context
Definition: Session.cs:80