public void StartLogging() { lock (this) { if (_server != null) { return; } try { // New server _server = new SyslogServer(SyslogServer.DefaultAddress, _port); // Register for events _server.SyslogMessageReceived += _eventDelegate; // Start listening _server.Connect(); } catch (Exception) { if (_server != null) { _server.Close(); _server.SyslogMessageReceived -= _eventDelegate; _server = null; } throw; } } }
private void Connect() { if (_server != null) { return; } try { IPAddress ip = _specificIP; if (ip == null) { ip = SyslogServer.DefaultAddress; } _server = new SyslogServer(ip, _port); _server.SyslogMessageReceived += new SyslogServer.SyslogMessageDelegate(OnSyslogMessage); _server.Connect(); } catch (Exception) { if (_server != null) { _server.Close(); _server = null; } throw; } }
[Test] public void CreateServer() { SyslogServer srv = new SyslogServer(); srv.Connect(); Assert.IsTrue(srv.Connected, "Server connected"); srv.Close(); }
[Test] public void CreateServerUsing() { using (SyslogServer srv = new SyslogServer()) { srv.Connect(); Assert.IsTrue(srv.Connected, "Server connected"); } }
[Test] public void CreateServerTwice() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); } using (SyslogServer srv2 = new SyslogServer()) { srv2.Connect(); } }
[Test] public void ServerInUse() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); using (SyslogServer srv2 = new SyslogServer()) { srv2.Connect(); } } }
public void ServerInUse() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); // Assert.That(() => srv1.Connect(), Throws.TypeOf<SocketException>()); using (SyslogServer srv2 = new SyslogServer()) { // srv2.Connect(); Assert.That(() => srv2.Connect(), Throws.TypeOf <SocketException>()); } } }
public void ServerInUse() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); // Assert.That(() => srv1.Connect(), Throws.TypeOf<SocketException>()); using (SyslogServer srv2 = new SyslogServer()) { // srv2.Connect(); Assert.That(() => srv2.Connect(), Throws.TypeOf<SocketException>()); } } }
[Test] public void TransferTest() { using (SyslogServer srv = new SyslogServer()) { srv.SyslogMessageReceived += new SyslogServer.SyslogMessageDelegate (OnSyslogMessageReceived); srv.Connect(); _messageCount = 0; SyslogMessage msg = new SyslogMessage("localhost", MessageText); using (SyslogUdpClient cl = new SyslogUdpClient()) { cl.Connect(IPAddress.Loopback); for (int i = 0; i < MessagesToSend; i++) { cl.Send(msg); // Allow a small delay so not to overload Thread.Sleep(10); } } // Sleep until message counts are settled int prevCount = -1; int newCount = 0; while (prevCount != newCount) { Thread.Sleep(100); lock (this) { prevCount = newCount; newCount = _messageCount; } } Assert.AreEqual(MessagesToSend, _messageCount, "Messages received"); } }
[Test] public void TransferTest() { using (SyslogServer srv = new SyslogServer()) { srv.SyslogMessageReceived += new SyslogServer.SyslogMessageDelegate (OnSyslogMessageReceived); srv.Connect(); _messageCount = 0; SyslogMessage msg = new SyslogMessage("localhost", MessageText); using (SyslogUdpClient cl = new SyslogUdpClient()) { cl.Connect(IPAddress.Loopback); for (int i=0; i<MessagesToSend; i++) { cl.Send(msg); // Allow a small delay so not to overload Thread.Sleep(10); } } // Sleep until message counts are settled int prevCount = -1; int newCount = 0; while (prevCount != newCount) { Thread.Sleep(100); lock (this) { prevCount = newCount; newCount = _messageCount; } } Assert.AreEqual(MessagesToSend, _messageCount, "Messages received"); } }
public void ServerInUse() { using (SyslogServer srv1 = new SyslogServer()) { srv1.Connect(); using (SyslogServer srv2 = new SyslogServer()) { srv2.Connect(); } } }