File: channel_manager_spec.rb

package info (click to toggle)
ruby-net-irc 0.0.9-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: ruby: 7,268; makefile: 3
file content (184 lines) | stat: -rwxr-xr-x 5,245 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!spec

$LOAD_PATH << "lib"
$LOAD_PATH << "../lib"

require "rubygems"
require "spec"
require "thread"
require "net/irc"
require "net/irc/client/channel_manager"
include Net::IRC
include Constants

class ChannelManagerTestServerSession < Net::IRC::Server::Session
	@@testq = SizedQueue.new(1)
	@@instance = nil

	def self.testq
		@@testq
	end

	def self.instance
		@@instance
	end

	def initialize(*args)
		super
		@@instance = self
	end

	def on_message(m)
		@@testq << m
	end
end

class ChannelManagerTestClient < Net::IRC::Client
	include Net::IRC::Client::ChannelManager
	@@testq = SizedQueue.new(1)

	def self.testq
		@@testq
	end

	def on_message(m)
		@@testq << m
	end
end

describe Net::IRC, "server and client" do
	before :all do
		@port = nil
		@server, @client = nil, nil

		Thread.abort_on_exception = true
		Thread.start do
			@server = Net::IRC::Server.new("localhost", @port, ChannelManagerTestServerSession, {
				:logger => Logger.new(nil),
			})
			@server.start
		end

		Thread.pass
		true until @server.instance_variable_get(:@serv)

		@port = @server.instance_variable_get(:@serv).addr[1]

		Thread.start do
			@client = ChannelManagerTestClient.new("localhost", @port, {
				:nick   => "foonick",
				:user   => "foouser",
				:real   => "foo real name",
				:pass   => "foopass",
				:logger => Logger.new(nil),
			})
			@client.start
		end

		Thread.pass
		true until @client
	end

	server_q = ChannelManagerTestServerSession.testq
	client_q = ChannelManagerTestClient.testq

	it "client should manage channel mode/users correctly" do
		client = @client
		client.instance_variable_set(:@prefix, Prefix.new("foonick!foouser@localhost"))

		true until ChannelManagerTestServerSession.instance
		ChannelManagerTestServerSession.instance.instance_eval do
			Thread.exclusive do
				post client.prefix,          JOIN,   "#test"
				post nil,                    NOTICE, "#test", "sep1"
			end
		end

		true until client_q.pop.to_s == "NOTICE #test sep1\r\n"

		c = @client.instance_variable_get(:@channels)
		c.synchronize do
			c.should                       be_a_kind_of(Hash)
			c["#test"].should              be_a_kind_of(Hash)
			c["#test"][:modes].should      be_a_kind_of(Array)
			c["#test"][:users].should      be_a_kind_of(Array)
			c["#test"][:users].should      == ["foonick"]
		end

		ChannelManagerTestServerSession.instance.instance_eval do
			Thread.exclusive do
				post "test1!test@localhost", JOIN,   "#test"
				post "test2!test@localhost", JOIN,   "#test"
				post nil,                    NOTICE, "#test", "sep2"
			end
		end

		true until client_q.pop.to_s == "NOTICE #test sep2\r\n"
		c.synchronize do
			c["#test"][:users].should      == ["foonick", "test1", "test2"]
		end

		ChannelManagerTestServerSession.instance.instance_eval do
			Thread.exclusive do
				post nil,                    RPL_NAMREPLY, client.prefix.nick, "@", "#test", "foo1 foo2 foo3 @foo4 +foo5"
				post nil,                    NOTICE, "#test", "sep3"
			end
		end

		true until client_q.pop.to_s == "NOTICE #test sep3\r\n"
		c.synchronize do
			c["#test"][:users].should      == ["foonick", "test1", "test2", "foo1", "foo2", "foo3", "foo4", "foo5"]
			c["#test"][:modes].should      include([:s, nil])
			c["#test"][:modes].should      include([:o, "foo4"])
			c["#test"][:modes].should      include([:v, "foo5"])
		end

		ChannelManagerTestServerSession.instance.instance_eval do
			Thread.exclusive do
				post nil,                    RPL_NAMREPLY, client.prefix.nick, "@", "#test1", "foo1 foo2 foo3 @foo4 +foo5"
				post "foo4!foo@localhost",   QUIT,   "message"
				post "foo5!foo@localhost",   PART,   "#test1", "message"
				post client.prefix,          KICK,   "#test", "foo1", "message"
				post client.prefix,          MODE,   "#test", "+o", "foo2"
				post nil,                    NOTICE, "#test", "sep4"
			end
		end

		true until client_q.pop.to_s == "NOTICE #test sep4\r\n"
		c.synchronize do
			c["#test"][:users].should      == ["foonick", "test1", "test2", "foo2", "foo3", "foo5"]
			c["#test1"][:users].should     == ["foo1", "foo2", "foo3"]
			c["#test"][:modes].should_not  include([:o, "foo4"])
			c["#test"][:modes].should      include([:v, "foo5"])
			c["#test1"][:modes].should_not include([:v, "foo5"])
			c["#test"][:modes].should      include([:o, "foo2"])
		end

		ChannelManagerTestServerSession.instance.instance_eval do
			Thread.exclusive do
				post "foonick!test@localhost",  NICK, "foonick2"
				post "foonick2!test@localhost", NICK, "foonick"
				post "foo2!test@localhost",     NICK, "bar2"
				post "foo3!test@localhost",     NICK, "bar3"
				post nil,                     NOTICE, "#test", "sep5"
			end
		end

		true until client_q.pop.to_s == "NOTICE #test sep5\r\n"
		c.synchronize do
			c["#test"][:users].should      == ["foonick", "test1", "test2", "bar2", "bar3", "foo5"]
			c["#test1"][:users].should     == ["foo1", "bar2", "bar3"]
			c["#test"][:modes].should_not  include([:o, "foo4"])
			c["#test"][:modes].should      include([:v, "foo5"])
			c["#test1"][:modes].should_not include([:v, "foo5"])
			c["#test"][:modes].should_not  include([:o, "foo2"])
			c["#test"][:modes].should      include([:o, "bar2"])
		end
	end

	after :all do
		@server.finish
		@client.finish
	end
end