1 require "#{File.dirname(__FILE__)}/../test_helper"
4 class TestEmitter < Test::Unit::TestCase
5 BEFORE_DELAY = ENV['BEFORE_DELAY'] ? ENV['BEFORE_DELAY'].to_f : 0.5
6 AFTER_DELAY = ENV['AFTER_DELAY'] ? ENV['AFTER_DELAY'].to_f : 0.5
9 assert_instance_of Module, LWES, "LWES is not a module"
10 assert_instance_of Class, LWES::Emitter, "LWES::Emitter is not a class"
13 def lwes_listener(&block)
14 cmd = "lwes-event-printing-listener" \
15 " -m #{@options[:address]}" \
16 " -i #{@options[:iface]}" \
17 " -p #{@options[:port]}"
18 out = Tempfile.new("out")
19 err = Tempfile.new("err")
23 $stdout.reopen(out.path)
24 $stderr.reopen(err.path)
28 # since everything executes asynchronously and our messaging,
29 # we need to ensure our listener is ready, then ensure our
30 # listener has printed something...
36 Process.kill(:TERM, pid)
38 assert_equal 0, err.size
45 :address => ENV["LWES_TEST_ADDRESS"] || "127.0.0.1",
46 :iface => ENV["LWES_TEST_IFACE"] || "0.0.0.0",
47 :port => ENV["LWES_TEST_PORT"] ? ENV["LWES_TEST_PORT"].to_i : 12345,
48 :ttl => 60, # nil for no ttl)
53 assert_instance_of LWES::Emitter, LWES::Emitter.new(@options)
56 def test_initialize_with_heartbeat
57 heartbeat = @options.merge(:heartbeat => 30)
58 assert_instance_of LWES::Emitter, LWES::Emitter.new(heartbeat)
61 def test_initialize_no_ttl
64 assert_instance_of LWES::Emitter, LWES::Emitter.new(no_ttl)
67 def test_initialize_invalid
68 assert_raises(TypeError) {
69 LWES::Emitter.new(@options.merge(:address => nil))
73 def test_initialize_empty_options
74 assert_raises(TypeError) { LWES::Emitter.new({}) }
78 emitter = LWES::Emitter.new(@options)
79 assert_raises(TypeError) { emitter.emit "Invalid", nil }
80 assert_raises(ArgumentError) { emitter.emit nil, { :hello => "world" }}
83 def test_emit_empty_hash
84 emitter = LWES::Emitter.new(@options)
85 assert_nothing_raised { emitter.emit("Valid", Hash.new) }
88 def test_emit_non_empty_hashes
89 emitter = LWES::Emitter.new(@options)
90 out = lwes_listener do
91 assert_nothing_raised {
92 emitter.emit("ASDF", { :foo => "FOO", :nr => [ :int16, 50 ] })
96 assert_match %r{\AASDF\b}, lines.first
97 assert ! lines.grep(/foo = FOO;/).empty?
98 assert ! lines.grep(/nr = 50;/).empty?
101 def test_emit_booleans
102 emitter = LWES::Emitter.new(@options)
103 event = { :true => true, :false => false }
104 out = lwes_listener do
105 assert_nothing_raised { emitter.emit("BOOLS", event)
108 lines = out.readlines
109 assert_equal 1, lines.grep(/true = true;/).size
110 assert_equal 1, lines.grep(/false = false;/).size
113 def test_emit_numeric_ranges
114 check_min_max(:int16, -0x7fff - 1, 0x7fff)
115 check_min_max(:int32, -0x7fffffff - 1, 0x7fffffff)
116 check_min_max(:int64, -0x7fffffffffffffff - 1, 0x7fffffffffffffff)
117 check_min_max(:uint16, 0, 0xffff)
118 check_min_max(:uint32, 0, 0xffffffff)
119 check_min_max(:uint64, 0, 0xffffffffffffffff)
122 def check_min_max(type, min, max)
123 emitter = LWES::Emitter.new(@options)
124 out = lwes_listener do
125 assert_raises(RangeError) {
126 emitter.emit("over", { type => [ type, max + 1] })
129 assert_raises(RangeError) {
130 emitter.emit("under", { type => [ type, min - 1 ] })
133 assert_nothing_raised {
134 emitter.emit("zero", { type => [ type, 0 ] })
136 assert_nothing_raised {
137 emitter.emit("min", { type => [ type, min ] })
139 assert_nothing_raised {
140 emitter.emit("max", { type => [ type, max ] })
143 lines = out.readlines
144 assert lines.grep(/\Aover/).empty?
145 assert lines.grep(/\Aunder/).empty?
146 assert_equal 1, lines.grep(/\Amax/).size
147 assert_equal 1, lines.grep(/\Amin/).size
148 assert_equal 1, lines.grep(/\Azero/).size
152 emitter = LWES::Emitter.new(@options)
153 assert_nil emitter.close