-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathspec_helper.rb
141 lines (116 loc) · 2.77 KB
/
spec_helper.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
# frozen_string_literal: true
require 'simplecov'
SimpleCov.start
require 'simplecov-cobertura'
SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
require 'segment/analytics'
require 'active_support/time'
# Setting timezone for ActiveSupport::TimeWithZone to UTC
Time.zone = 'UTC'
module Segment
class Analytics
WRITE_KEY = 'testsecret'
TRACK = {
:event => 'Ruby Library test event',
:properties => {
:type => 'Chocolate',
:is_a_lie => true,
:layers => 20,
:created => Time.new
}
}
IDENTIFY = {
:traits => {
:likes_animals => true,
:instrument => 'Guitar',
:age => 25
}
}
ALIAS = {
:previous_id => 1234,
:user_id => 'abcd'
}
GROUP = {}
PAGE = {}
SCREEN = {
:name => 'main'
}
USER_ID = 1234
GROUP_ID = 1234
# Hashes sent to the client, snake_case
module Queued
TRACK = TRACK.merge :user_id => USER_ID
IDENTIFY = IDENTIFY.merge :user_id => USER_ID
GROUP = GROUP.merge :group_id => GROUP_ID, :user_id => USER_ID
PAGE = PAGE.merge :user_id => USER_ID
SCREEN = SCREEN.merge :user_id => USER_ID
end
# Hashes which are sent from the worker, camel_cased
module Requested
TRACK = TRACK.merge({
:userId => USER_ID,
:type => 'track'
})
IDENTIFY = IDENTIFY.merge({
:userId => USER_ID,
:type => 'identify'
})
GROUP = GROUP.merge({
:groupId => GROUP_ID,
:userId => USER_ID,
:type => 'group'
})
PAGE = PAGE.merge :userId => USER_ID
SCREEN = SCREEN.merge :userId => USER_ID
end
end
end
# A worker that doesn't consume jobs
class NoopWorker
def run
# Does nothing
end
end
# A worker that consumes all jobs
class DummyWorker
def initialize(queue)
@queue = queue
end
def run
@queue.pop until @queue.empty?
end
def is_requesting?
false
end
end
# A backoff policy that returns a fixed list of values
class FakeBackoffPolicy
def initialize(interval_values)
@interval_values = interval_values
end
def next_interval
raise 'FakeBackoffPolicy has no values left' if @interval_values.empty?
@interval_values.shift
end
end
# usage:
# it "should return a result of 5" do
# eventually(options: {timeout: 1}) { long_running_thing.result.should eq(5) }
# end
module AsyncHelper
def eventually(options = {})
timeout = options[:timeout] || 2
interval = options[:interval] || 0.1
time_limit = Time.now + timeout
loop do
begin
yield
return
rescue RSpec::Expectations::ExpectationNotMetError => error
raise error if Time.now >= time_limit
sleep interval
end
end
end
end
include AsyncHelper