-
Notifications
You must be signed in to change notification settings - Fork 388
/
session_spec.rb
231 lines (199 loc) · 7.75 KB
/
session_spec.rb
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
require ::File.expand_path('../spec_helper.rb', __FILE__)
describe FnordMetric::Session do
include FnordMetric
before(:all) do
@now = Time.utc(1992,01,13,5,23,23).to_i
@redis = Redis.new
@redis_wrap = RedisWrap.new(@redis)
@namespace = "fnordmetric-ns123"
@sessions = "#{@namespace}-session"
end
describe "creating sessions" do
before(:all) do
@event = { :_session => "sess123", :_time => @now, :_eid => "34089749" }
@md5_key = Digest::MD5.hexdigest("sess123")
@redis.del("#{@namespace}-session")
@redis.del("#{@namespace}-session-#{@md5_key}-events")
@redis.del("#{@namespace}-session-#{@md5_key}-data")
end
it "should add a new session on intialize" do
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => @event,
:session_data_ttl => 10,
:redis => @redis_wrap
)
@redis.zcard(@sessions).should == 1
end
it "should add a new session on intialize and hash the session token" do
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => @event,
:session_data_ttl => 10,
:redis => @redis_wrap
)
@redis.zrange(@sessions, 0, -1).should == [ @md5_key]
end
it "should add a new session on intialize and set the timestamp as score" do
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => @event,
:session_data_ttl => 10,
:redis => @redis_wrap
)
@redis.zscore(@sessions, @md5_key).to_i.should == @now
end
it "should update the timestamp on a existing session" do
@redis.zadd(@sessions, @now-10, @md5_key)
@redis.zscore(@sessions, @md5_key).to_i.should == @now-10
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => @event,
:session_data_ttl => 10,
:redis => @redis_wrap
)
@redis.zscore(@sessions, @md5_key).to_i.should == @now
end
it "should add the event_id to the session-event set on a new session" do
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => @event,
:session_data_ttl => 10,
:redis => @redis_wrap
)
events_key = "#{@namespace}-session-#{@md5_key}-events"
@redis.zrange(events_key, 0, -1).first.should == @event[:_eid]
end
it "should store a name in the session data" do
event_data = @event.merge(
:_type => "_set_name",
:name => "Horst Mayer"
)
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => event_data,
:session_data_ttl => 10,
:redis => @redis_wrap
)
data_key = "#{@namespace}-session-#{@md5_key}-data"
@redis.hget(data_key, "_name").should == "Horst Mayer"
end
it "should store a picture in the session data" do
event_data = @event.merge(
:_type => "_set_picture",
:url => "http://myhost/mypic.jpg"
)
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => event_data,
:session_data_ttl => 10,
:redis => @redis_wrap
)
data_key = "#{@namespace}-session-#{@md5_key}-data"
@redis.hget(data_key, "_picture").should == "http://myhost/mypic.jpg"
end
it "should store arbitrary data in the session data" do
event_data = @event.merge(
:_type => "_set_data",
:fnord => "blubb",
:foobar => "123"
)
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => event_data,
:session_data_ttl => 10,
:redis => @redis_wrap
)
data_key = "#{@namespace}-session-#{@md5_key}-data"
@redis.hget(data_key, "fnord").should == "blubb"
@redis.hget(data_key, "foobar").should == "123"
end
it "not store special attributes in the session" do
event_data = @event.merge(
:_type => "_set_data",
:fnord => "blubb",
:foobar => "123"
)
FnordMetric::Session.create(
:namespace_prefix => @namespace,
:event => event_data,
:session_data_ttl => 10,
:redis => @redis_wrap
)
data_key = "#{@namespace}-session-#{@md5_key}-data"
@redis.hget(data_key, "_time").should be_nil
@redis.hget(data_key, "_eid").should be_nil
end
end
describe "Finding Sessions" do
before(:each) do
@redis.del("#{@namespace}-session")
@redis.keys("#{@namespace}-session-*").each { |k| @redis.del(k) }
@opts = {
:redis_prefix => "#{@namespace}-session",
:namespace_prefix => "#{@namespace}",
:redis => @redis
}
end
it "should find all sessions" do
create_session("sess533", @now, {})
create_session("sess343", @now, {})
FnordMetric::Session.all(@opts).length.should == 2
end
it "should find all sessions and return session objects" do
create_session("sess523", @now, {})
FnordMetric::Session.all(@opts).first.should be_a(FnordMetric::Session)
end
it "should find a session and return a session object" do
create_session("sess223", @now, {})
sess = FnordMetric::Session.find(Digest::MD5.hexdigest("sess223"), @opts)
sess.should be_a(FnordMetric::Session)
sess.session_key.should == Digest::MD5.hexdigest("sess223")
end
it "should find a sessions and return a session object with data" do
create_session("sess123", @now, { :fnord => "blubb" })
sess = FnordMetric::Session.find(Digest::MD5.hexdigest("sess123"), @opts)
sess.fetch_data!
sess.data(:fnord).should == "blubb"
end
it "should not include special attributes in data" do
event_data = { :_name => "Horst Mayer", :_picture => "http://myhost/mypic.jpg" }
create_session("sess173", @now, event_data)
sess = FnordMetric::Session.find(Digest::MD5.hexdigest("sess173"), @opts)
sess.fetch_data!
sess.data(:_name).should == nil
sess.data(:_picture).should == nil
end
it "should find a session and return a session object with picture" do
event_data = { :_name => "Horst Mayer", :_picture => "http://myhost/mypic.jpg" }
create_session("sess163", @now, event_data)
sess = FnordMetric::Session.find(Digest::MD5.hexdigest("sess163"), @opts)
sess.fetch_data!
sess.picture.should == "http://myhost/mypic.jpg"
end
it "should find a session and return a session object with name" do
event_data = { :_name => "Horst Mayer", :_picture => "http://myhost/mypic.jpg" }
create_session("sess143", @now, event_data)
sess = FnordMetric::Session.find(Digest::MD5.hexdigest("sess143"), @opts)
sess.fetch_data!
sess.name.should == "Horst Mayer"
end
it "should find a session and return a session object with event_ids" do
sesshash = create_session("sess923", @now, {})
@redis_wrap.zadd("#{@namespace}-session-#{sesshash}-events", @now, "shmoo")
@redis_wrap.zadd("#{@namespace}-session-#{sesshash}-events", @now, "fnord")
sess = FnordMetric::Session.find(sesshash, @opts)
sess.fetch_event_ids!
sess.event_ids.should include("shmoo")
sess.event_ids.should include("fnord")
end
def create_session(sesskey, sesstime, sessdata)
Digest::MD5.hexdigest(sesskey).tap do |sesshash|
@redis_wrap.zadd("#{@namespace}-session", sesstime, sesshash)
sessdata.each do |k,v|
@redis_wrap.hset("#{@namespace}-session-#{sesshash}-data", k, v)
end
end
end
end
end