aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlex Young <alex@bytemark.co.uk>2015-02-24 11:34:38 +0000
committerAlex Young <alex@bytemark.co.uk>2015-02-24 11:34:38 +0000
commitc12e2c8f65a59a075ad20424a03d137a62db7e48 (patch)
treecea2ad83471c161aa099a8279ef60f7a04a83d92 /test
parent16ba9f08109ddb911e9aba8518a93d3df710a46b (diff)
Add some basic UDPServer tests.
Diffstat (limited to 'test')
-rw-r--r--test/tc_mauve_udp_server.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/test/tc_mauve_udp_server.rb b/test/tc_mauve_udp_server.rb
new file mode 100644
index 0000000..9a82de8
--- /dev/null
+++ b/test/tc_mauve_udp_server.rb
@@ -0,0 +1,54 @@
+# encoding: utf-8
+
+$:.unshift "../lib"
+
+require 'th_mauve'
+require 'th_mauve_resolv'
+require 'mauve/udp_server'
+require 'mauve/server'
+
+class TcMauveUdpServer < Mauve::UnitTest
+ # Note: this test fires up the UDP socket. It *will* fail if more
+ # than one instance runs at the same time on the same machine.
+ include Mauve
+
+ def setup
+ super
+ Server.instance.packet_buffer.clear # blarg!
+ @server = UDPServer.instance # blarg!
+ end
+
+ def test_listens
+ update = generic_update()
+ t = Thread.new do @server.__send__(:main_loop) end
+ sleep(0.2)
+ sender.send(update)
+ Timeout.timeout(2) do t.join end
+ assert_equal update, Proto::AlertUpdate.new.parse_from_string(Server.packet_pop[0])
+ end
+
+ def test_closes
+ update = generic_update()
+ t = @server.run
+ sleep(0.2)
+ @server.stop
+ sleep(0.2)
+ sender.send(update)
+ Timeout.timeout(2) do t.join end
+ assert_nil Server.packet_pop
+ end
+
+
+ def sender
+ Sender.new(["#{@server.ip}:#{@server.port}"])
+ end
+
+ def generic_update
+ update = Proto::AlertUpdate.new
+ alert = Proto::Alert.new
+ alert.id = "alertid"
+ update.alert << alert
+ update
+ end
+
+end