copy_tuner_client 0.4.9 → 0.4.10

Sign up to get free protection for your applications and to get access to all the features.
data/package.json CHANGED
@@ -4,24 +4,27 @@
4
4
  "repository": "https://github.com/SonicGarden/copy-tuner-ruby-client",
5
5
  "author": "SonicGarden",
6
6
  "license": "MIT",
7
+ "engines": {
8
+ "node": "8.x"
9
+ },
7
10
  "scripts": {
8
11
  "build": "rollup -c",
9
12
  "watch": "rollup -c -w"
10
13
  },
11
- "dependencies": {
12
- "keycode-js": "0.0.4",
13
- "lodash.debounce": "^4.0.8"
14
- },
14
+ "dependencies": {},
15
15
  "devDependencies": {
16
+ "babel-core": "^6.26.3",
16
17
  "babel-plugin-external-helpers": "^6.22.0",
17
- "babel-preset-env": "^1.6.0",
18
- "eslint": "^3.19.0",
19
- "eslint-config-airbnb-base": "^11.2.0",
20
- "eslint-plugin-import": "^2.7.0",
21
- "rollup": "^0.45.2",
22
- "rollup-plugin-babel": "^2.7.1",
23
- "rollup-plugin-commonjs": "^8.0.2",
24
- "rollup-plugin-node-resolve": "^3.0.0",
18
+ "babel-preset-env": "^1.7.0",
19
+ "eslint": "^4.19.1",
20
+ "eslint-config-airbnb-base": "^13.0.0",
21
+ "eslint-plugin-import": "^2.13.0",
22
+ "keycode-js": "^1.0.0",
23
+ "lodash.debounce": "^4.0.8",
24
+ "rollup": "^0.62.0",
25
+ "rollup-plugin-babel": "^3.0.7",
26
+ "rollup-plugin-commonjs": "^9.1.3",
27
+ "rollup-plugin-node-resolve": "^3.3.0",
25
28
  "rollup-watch": "^4.3.1"
26
29
  }
27
30
  }
data/rollup.config.js CHANGED
@@ -3,9 +3,11 @@ import resolve from 'rollup-plugin-node-resolve';
3
3
  import commonjs from 'rollup-plugin-commonjs';
4
4
 
5
5
  export default {
6
- entry: 'src/main.js',
7
- dest: 'app/assets/javascripts/copyray.js',
8
- format: 'iife',
6
+ input: 'src/main.js',
7
+ output: {
8
+ file: 'app/assets/javascripts/copyray.js',
9
+ format: 'iife',
10
+ },
9
11
  plugins: [
10
12
  resolve({ jsnext: true, main: true }),
11
13
  commonjs(),
@@ -98,7 +98,7 @@ describe CopyTunerClient::Cache do
98
98
  it "handles connection errors when flushing" do
99
99
  failure = "server is napping"
100
100
  logger = FakeLogger.new
101
- client.stubs(:upload).raises(CopyTunerClient::ConnectionError.new(failure))
101
+ expect(client).to receive(:upload).and_raise(CopyTunerClient::ConnectionError.new(failure))
102
102
  cache = build_cache(:logger => logger)
103
103
  cache['upload.key'] = 'upload'
104
104
 
@@ -110,7 +110,7 @@ describe CopyTunerClient::Cache do
110
110
  it "handles connection errors when downloading" do
111
111
  failure = "server is napping"
112
112
  logger = FakeLogger.new
113
- client.stubs(:download).raises(CopyTunerClient::ConnectionError.new(failure))
113
+ expect(client).to receive(:download).and_raise(CopyTunerClient::ConnectionError.new(failure))
114
114
  cache = build_cache(:logger => logger)
115
115
 
116
116
  cache.download
@@ -120,43 +120,46 @@ describe CopyTunerClient::Cache do
120
120
 
121
121
  it "blocks until the first download is complete" do
122
122
  logger = FakeLogger.new
123
- logger.stubs(:flush)
124
- client.delay = 0.5
123
+ expect(logger).to receive(:flush)
124
+ client.delay = true
125
125
  cache = build_cache(:logger => logger)
126
126
 
127
- Thread.new { cache.download }
127
+ t_download = Thread.new { cache.download }
128
+ sleep 0.1 until cache.pending?
128
129
 
129
- finished = false
130
- Thread.new do
130
+ t_wait = Thread.new do
131
131
  cache.wait_for_download
132
- finished = true
133
132
  end
134
-
135
- sleep(1)
136
-
137
- expect(finished).to eq(true)
138
- # FIXME 成功したり、失敗していたりするので、一旦コメントアウト。後で直します。
139
- # logger.should have_entry(:info, "Waiting for first download")
140
- # logger.should have_received(:flush)
133
+ sleep 0.1 until logger.has_entry?(:info, "Waiting for first download")
134
+ client.go
135
+ expect(t_download.join(1)).not_to be_nil
136
+ expect(cache.pending?).to be_falsey
137
+ expect(t_wait.join(1)).not_to be_nil
141
138
  end
142
139
 
143
140
  it "doesn't block if the first download fails" do
144
- client.delay = 0.5
141
+ client.delay = true
145
142
  client.error = StandardError.new("Failure")
146
143
  cache = build_cache
147
144
 
148
- Thread.new { cache.download }
145
+ error = nil
146
+ t_download = Thread.new do
147
+ begin
148
+ cache.download
149
+ rescue StandardError => e
150
+ error = e
151
+ end
152
+ end
153
+ sleep 0.1 until cache.pending?
149
154
 
150
- finished = false
151
- Thread.new do
155
+ t_wait = Thread.new do
152
156
  cache.wait_for_download
153
- finished = true
154
157
  end
155
-
156
- sleep(1)
157
-
158
+ client.go
159
+ expect(t_download.join(1)).not_to be_nil
160
+ expect(error).to be_kind_of(StandardError)
161
+ expect(t_wait.join(1)).not_to be_nil
158
162
  expect { cache.download }.to raise_error(StandardError, "Failure")
159
- expect(finished).to eq(true)
160
163
  end
161
164
 
162
165
  it "doesn't block before downloading" do
@@ -218,7 +221,7 @@ describe CopyTunerClient::Cache do
218
221
 
219
222
  before do
220
223
  mutex.lock
221
- Mutex.stubs(:new => mutex)
224
+ allow(Mutex).to receive(:new).and_return(mutex)
222
225
  end
223
226
 
224
227
  it "synchronizes read access to keys between threads" do
@@ -239,12 +242,9 @@ describe CopyTunerClient::Cache do
239
242
  CopyTunerClient.configure do |config|
240
243
  config.cache = cache
241
244
  end
242
- cache.stubs(:flush)
245
+ expect(cache).to receive(:flush).at_least(:once)
243
246
 
244
247
  CopyTunerClient.flush
245
-
246
- # FIXME 不安定なので後ほど修正する。
247
- # cache.should have_received(:flush)
248
248
  end
249
249
 
250
250
  describe "#export" do
@@ -260,11 +260,9 @@ describe CopyTunerClient::Cache do
260
260
  CopyTunerClient.configure do |config|
261
261
  config.cache = @cache
262
262
  end
263
- @cache.stubs(:export)
263
+ expect(@cache).to receive(:export)
264
264
 
265
265
  CopyTunerClient.export
266
-
267
- expect(@cache).to have_received(:export)
268
266
  end
269
267
 
270
268
  it "returns no yaml with no blurb keys" do
@@ -24,7 +24,7 @@ describe CopyTunerClient do
24
24
  let(:http) { Net::HTTP.new(config.host, config.port) }
25
25
 
26
26
  before do
27
- Net::HTTP.stubs(:new => http)
27
+ allow(Net::HTTP).to receive(:new).and_return(http)
28
28
  end
29
29
 
30
30
  it 'should timeout when connecting' do
@@ -71,7 +71,7 @@ describe CopyTunerClient do
71
71
  ]
72
72
 
73
73
  errors.each do |original_error|
74
- http.stubs(:request).raises(original_error)
74
+ allow(http).to receive(:request).and_raise(original_error)
75
75
  client = build_client_with_project
76
76
  expect { client.download { |ignore| } }.
77
77
  to raise_error(CopyTunerClient::ConnectionError) { |error|
@@ -200,11 +200,9 @@ describe CopyTunerClient do
200
200
  CopyTunerClient.configure do |config|
201
201
  config.client = client
202
202
  end
203
- client.stubs(:deploy)
203
+ expect(client).to receive(:deploy)
204
204
 
205
205
  CopyTunerClient.deploy
206
-
207
- expect(client).to have_received(:deploy)
208
206
  end
209
207
 
210
208
  it "deploys" do
@@ -162,9 +162,8 @@ describe CopyTunerClient::Configuration do
162
162
 
163
163
  it 'logs to $stdout by default' do
164
164
  logger = FakeLogger.new
165
- Logger.stubs :new => logger
165
+ expect(Logger).to receive(:new).with($stdout).and_return(logger)
166
166
  config = CopyTunerClient::Configuration.new
167
- expect(Logger).to have_received(:new).with($stdout)
168
167
  expect(config.logger.original_logger).to eq(logger)
169
168
  end
170
169
 
@@ -194,19 +193,19 @@ end
194
193
 
195
194
  shared_context 'stubbed configuration' do
196
195
  subject { CopyTunerClient::Configuration.new }
197
- let(:backend) { stub('i18n-backend') }
198
- let(:cache) { stub('cache', :download => "download") }
199
- let(:client) { stub('client') }
196
+ let(:backend) { double('i18n-backend') }
197
+ let(:cache) { double('cache', download: "download") }
198
+ let(:client) { double('client') }
200
199
  let(:logger) { FakeLogger.new }
201
- let(:poller) { stub('poller') }
202
- let(:process_guard) { stub('process_guard', :start => nil) }
200
+ let(:poller) { double('poller') }
201
+ let(:process_guard) { double('process_guard', start: nil) }
203
202
 
204
203
  before do
205
- CopyTunerClient::I18nBackend.stubs :new => backend
206
- CopyTunerClient::Client.stubs :new => client
207
- CopyTunerClient::Cache.stubs :new => cache
208
- CopyTunerClient::Poller.stubs :new => poller
209
- CopyTunerClient::ProcessGuard.stubs :new => process_guard
204
+ allow(CopyTunerClient::I18nBackend).to receive(:new).and_return(backend)
205
+ allow(CopyTunerClient::Client).to receive(:new).and_return(client)
206
+ allow(CopyTunerClient::Cache).to receive(:new).and_return(cache)
207
+ allow(CopyTunerClient::Poller).to receive(:new).and_return(poller)
208
+ allow(CopyTunerClient::ProcessGuard).to receive(:new).and_return(process_guard)
210
209
  subject.logger = logger
211
210
  apply
212
211
  end
@@ -243,7 +242,7 @@ end
243
242
  describe CopyTunerClient::Configuration, 'applied when testing' do
244
243
  it_should_behave_like 'applied configuration' do
245
244
  it 'does not start the process guard' do
246
- expect(process_guard).to have_received(:start).never
245
+ expect(process_guard).not_to receive(:start)
247
246
  end
248
247
  end
249
248
 
@@ -334,13 +333,16 @@ describe CopyTunerClient::Configuration, 'applied with locale filter' do
334
333
  end
335
334
 
336
335
  describe CopyTunerClient::Configuration, 'applied with Rails i18n config' do
336
+ let!(:rails_defined) { Object.const_defined?(:Rails) }
337
+
337
338
  def self.with_config(i18n_options)
338
- around do |ex|
339
- rails_defined = Object.const_defined?(:Rails)
339
+ before do
340
340
  Object.const_set :Rails, Module.new unless rails_defined
341
- i18n = stub(i18n_options)
342
- Rails.stubs application: stub(config: stub(i18n: i18n))
343
- ex.run
341
+ i18n = double('i18n', i18n_options)
342
+ allow(Rails).to receive_message_chain(:application, :config, :i18n) { i18n }
343
+ end
344
+
345
+ after do
344
346
  Object.send(:remove_const, :Rails) unless rails_defined
345
347
  end
346
348
  end
@@ -11,7 +11,7 @@ describe CopyTunerClient::I18nBackend do
11
11
 
12
12
  before do
13
13
  @default_backend = I18n.backend
14
- cache.stubs(:wait_for_download)
14
+ allow(cache).to receive(:wait_for_download)
15
15
  end
16
16
 
17
17
  after { I18n.backend = @default_backend }
@@ -19,12 +19,10 @@ describe CopyTunerClient::I18nBackend do
19
19
  subject { build_backend }
20
20
 
21
21
  it "reloads locale files and waits for the download to complete" do
22
- I18n.stubs(:load_path => [])
22
+ expect(I18n).to receive(:load_path).and_return([])
23
+ expect(cache).to receive(:wait_for_download)
23
24
  subject.reload!
24
25
  subject.translate('en', 'test.key', :default => 'something')
25
-
26
- expect(cache).to have_received(:wait_for_download)
27
- expect(I18n).to have_received(:load_path)
28
26
  end
29
27
 
30
28
  it "includes the base i18n backend" do
@@ -41,8 +39,8 @@ describe CopyTunerClient::I18nBackend do
41
39
  end
42
40
 
43
41
  it "finds available locales from locale files and cache" do
44
- YAML.stubs(:load_file => { 'es' => { 'key' => 'value' } })
45
- I18n.stubs(:load_path => ["test.yml"])
42
+ allow(YAML).to receive(:load_file).and_return({ 'es' => { 'key' => 'value' } })
43
+ allow(I18n).to receive(:load_path).and_return(["test.yml"])
46
44
 
47
45
  cache['en.key'] = ''
48
46
  cache['fr.key'] = ''
@@ -61,7 +61,8 @@ describe CopyTunerClient::Poller do
61
61
  it "stops polling with an invalid api key" do
62
62
  failure = "server is napping"
63
63
  logger = FakeLogger.new
64
- cache.stubs(:download).raises(CopyTunerClient::InvalidApiKey.new(failure))
64
+
65
+ expect(cache).to receive(:download).and_raise(CopyTunerClient::InvalidApiKey.new(failure))
65
66
  poller = build_poller(:logger => logger)
66
67
 
67
68
  cache['upload.key'] = 'upload'
@@ -77,7 +78,7 @@ describe CopyTunerClient::Poller do
77
78
  end
78
79
 
79
80
  it "logs an error if the background thread can't start" do
80
- Thread.stubs(:new => nil)
81
+ expect(Thread).to receive(:new).and_return(nil)
81
82
  logger = FakeLogger.new
82
83
 
83
84
  build_poller(:logger => logger).start
@@ -87,12 +88,10 @@ describe CopyTunerClient::Poller do
87
88
 
88
89
  it "flushes the log when polling" do
89
90
  logger = FakeLogger.new
90
- logger.stubs(:flush)
91
+ expect(logger).to receive(:flush).at_least(:once)
91
92
 
92
93
  build_poller(:logger => logger).start
93
94
 
94
95
  wait_for_next_sync
95
-
96
- expect(logger).to have_received(:flush).at_least_once
97
96
  end
98
97
  end
@@ -24,11 +24,9 @@ describe CopyTunerClient::PrefixedLogger do
24
24
  end
25
25
 
26
26
  it "calls flush for a logger that responds to flush" do
27
- output_logger.stubs(:flush)
27
+ expect(output_logger).to receive(:flush)
28
28
 
29
29
  subject.flush
30
-
31
- expect(output_logger).to have_received(:flush)
32
30
  end
33
31
 
34
32
  it "doesn't call flush for a logger that doesn't respond to flush" do
@@ -11,24 +11,26 @@ describe CopyTunerClient::ProcessGuard do
11
11
  $0 = @original_process_name
12
12
  end
13
13
 
14
- let(:cache) { stub('cache', :flush => nil) }
15
- let(:poller) { stub('poller', :start => nil) }
14
+ let(:cache) { double('cache', flush: nil) }
15
+ let(:poller) { double('poller', start: nil) }
16
16
 
17
17
  def build_process_guard(options = {})
18
+ preserve_exit_hook = options.delete(:preserve_exit_hook)
18
19
  options[:logger] ||= FakeLogger.new
19
20
  options[:cache] ||= cache
20
- CopyTunerClient::ProcessGuard.new(options[:cache], poller, options)
21
+ process_guard = CopyTunerClient::ProcessGuard.new(options[:cache], poller, options)
22
+ allow(process_guard).to receive(:register_exit_hooks) unless preserve_exit_hook
23
+ process_guard
21
24
  end
22
25
 
23
26
  it "starts polling from a worker process" do
27
+ expect(poller).to receive(:start)
24
28
  process_guard = build_process_guard
25
-
26
29
  process_guard.start
27
-
28
- expect(poller).to have_received(:start)
29
30
  end
30
31
 
31
32
  it "registers passenger hooks from the passenger master" do
33
+ expect(poller).not_to receive(:start)
32
34
  logger = FakeLogger.new
33
35
  passenger = define_constant('PhusionPassenger', FakePassenger.new)
34
36
  passenger.become_master
@@ -37,10 +39,10 @@ describe CopyTunerClient::ProcessGuard do
37
39
  process_guard.start
38
40
 
39
41
  expect(logger).to have_entry(:info, "Registered Phusion Passenger fork hook")
40
- expect(poller).to have_received(:start).never
41
42
  end
42
43
 
43
44
  it "starts polling from a passenger worker" do
45
+ expect(poller).to receive(:start)
44
46
  logger = FakeLogger.new
45
47
  passenger = define_constant('PhusionPassenger', FakePassenger.new)
46
48
  passenger.become_master
@@ -48,11 +50,10 @@ describe CopyTunerClient::ProcessGuard do
48
50
 
49
51
  process_guard.start
50
52
  passenger.spawn
51
-
52
- expect(poller).to have_received(:start)
53
53
  end
54
54
 
55
55
  it "registers unicorn hooks from the unicorn master" do
56
+ expect(poller).not_to receive(:start)
56
57
  logger = FakeLogger.new
57
58
  define_constant('Unicorn', Module.new)
58
59
  http_server = Class.new(FakeUnicornServer)
@@ -63,10 +64,10 @@ describe CopyTunerClient::ProcessGuard do
63
64
  process_guard.start
64
65
 
65
66
  expect(logger).to have_entry(:info, "Registered Unicorn fork hook")
66
- expect(poller).to have_received(:start).never
67
67
  end
68
68
 
69
69
  it "starts polling from a unicorn worker" do
70
+ expect(poller).to receive(:start)
70
71
  logger = FakeLogger.new
71
72
  define_constant('Unicorn', Module.new)
72
73
  http_server = Class.new(FakeUnicornServer)
@@ -76,14 +77,12 @@ describe CopyTunerClient::ProcessGuard do
76
77
 
77
78
  process_guard.start
78
79
  unicorn.spawn
79
-
80
- expect(poller).to have_received(:start)
81
80
  end
82
81
 
83
82
  it "flushes when the process terminates" do
84
83
  cache = WritingCache.new
85
84
  pid = fork do
86
- process_guard = build_process_guard(:cache => cache)
85
+ process_guard = build_process_guard(cache: cache, preserve_exit_hook: true)
87
86
  process_guard.start
88
87
  exit
89
88
  end
@@ -5,16 +5,16 @@ describe CopyTunerClient::RequestSync do
5
5
  let(:cache) { {} }
6
6
  let(:response) { 'response' }
7
7
  let(:env) { 'env' }
8
- let(:app) { stub('app', :call => response) }
8
+ let(:app) { double('app', :call => response) }
9
9
  before do
10
- cache.stubs(:flush => nil, :download => nil)
11
- poller.stubs(:start_sync => nil)
10
+ allow(cache).to receive_messages(:flush => nil, :download => nil)
11
+ allow(poller).to receive(:start_sync).and_return(nil)
12
12
  end
13
13
  subject { CopyTunerClient::RequestSync.new(app, :poller => poller, :cache => cache, :interval => 0) }
14
14
 
15
15
  it "invokes the upstream app" do
16
+ expect(app).to receive(:call).with(env)
16
17
  result = subject.call(env)
17
- expect(app).to have_received(:call).with(env)
18
18
  expect(result).to eq(response)
19
19
  end
20
20
  end
@@ -26,18 +26,18 @@ describe CopyTunerClient::RequestSync, 'serving assets' do
26
26
  let(:poller) { {} }
27
27
  let(:cache) { {} }
28
28
  let(:response) { 'response' }
29
- let(:app) { stub('app', :call => response) }
29
+ let(:app) { double('app', :call => response) }
30
30
  before do
31
- cache.stubs(:flush => nil, :download => nil)
32
- poller.stubs(:start_sync => nil)
31
+ allow(cache).to receive_messages(:flush => nil, :download => nil)
32
+ allow(poller).to receive(:start_sync).and_return(nil)
33
33
  end
34
34
  subject { CopyTunerClient::RequestSync.new(app, :poller => poller, :cache => cache, :interval => 0) }
35
35
 
36
36
  it "don't start sync" do
37
+ expect(cache).to receive(:download).once
37
38
  subject.call(env)
38
- expect(cache).to have_received(:download).once
39
+ expect(poller).not_to receive(:start_sync)
39
40
  subject.call(env)
40
- expect(poller).to have_received(:start_sync).never
41
41
  end
42
42
  end
43
43
 
@@ -46,36 +46,38 @@ describe CopyTunerClient::RequestSync do
46
46
  let(:cache) { {} }
47
47
  let(:response) { 'response' }
48
48
  let(:env) { 'env' }
49
- let(:app) { stub('app', :call => response) }
49
+ let(:app) { double('app', :call => response) }
50
50
  subject { CopyTunerClient::RequestSync.new(app, :poller => poller, :cache => cache, :interval => 10) }
51
51
  before do
52
- cache.stubs(:flush => nil, :download => nil)
53
- poller.stubs(:start_sync => nil)
52
+ allow(cache).to receive_messages(:flush => nil, :download => nil)
53
+ allow(poller).to receive(:start_sync).and_return(nil)
54
54
  end
55
55
 
56
56
  context "first request" do
57
57
  it "download" do
58
+ expect(cache).to receive(:download).once
58
59
  subject.call(env)
59
- expect(cache).to have_received(:download).once
60
60
  end
61
61
  end
62
62
 
63
63
  context 'in interval request' do
64
64
  it "does not start sync for the second time" do
65
+ expect(cache).to receive(:download).once
65
66
  subject.call(env)
66
- expect(cache).to have_received(:download).once
67
+
68
+ expect(poller).not_to receive(:start_sync)
67
69
  subject.call(env)
68
- expect(poller).to have_received(:start_sync).never
69
70
  end
70
71
  end
71
72
 
72
73
  context 'over interval request' do
73
74
  it "start sync for the second time" do
75
+ expect(cache).to receive(:download).once
74
76
  subject.call(env)
75
- expect(cache).to have_received(:download).once
77
+
78
+ expect(poller).to receive(:start_sync).once
76
79
  subject.last_synced = Time.now - 60
77
80
  subject.call(env)
78
- expect(poller).to have_received(:start_sync).once
79
81
  end
80
82
  end
81
83
  end