|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | #
|
4 |
| -# Copyright 2019, Optimizely and contributors |
| 4 | +# Copyright 2019-2020, Optimizely and contributors |
5 | 5 | #
|
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License");
|
7 | 7 | # you may not use this file except in compliance with the License.
|
|
17 | 17 | #
|
18 | 18 | require 'spec_helper'
|
19 | 19 | require 'optimizely/config_manager/async_scheduler'
|
| 20 | +require 'optimizely/error_handler' |
20 | 21 | require 'optimizely/logger'
|
21 | 22 |
|
22 | 23 | describe Optimizely::AsyncScheduler do
|
| 24 | + let(:error_handler) { Optimizely::RaiseErrorHandler.new } |
| 25 | + let(:spy_logger) { spy('logger') } |
| 26 | + |
23 | 27 | it 'should log error trace when callback fails to execute' do
|
| 28 | + allow(Thread).to receive(:new).and_yield |
24 | 29 | def some_callback(_args); end
|
25 | 30 |
|
26 |
| - spy_logger = spy('logger') |
27 |
| - |
28 |
| - scheduler = Optimizely::AsyncScheduler.new(method(:some_callback), 10, false, spy_logger) |
29 |
| - scheduler.start! |
| 31 | + scheduler = Optimizely::AsyncScheduler.new(method(:some_callback), 10, false, spy_logger, error_handler) |
30 | 32 |
|
31 |
| - while scheduler.running; end |
| 33 | + expect { scheduler.start! }.to raise_error(StandardError) |
32 | 34 | expect(spy_logger).to have_received(:log).with(
|
33 | 35 | Logger::ERROR,
|
34 | 36 | 'Something went wrong when executing passed callback. wrong number of arguments (given 0, expected 1)'
|
35 | 37 | )
|
36 | 38 | end
|
| 39 | + |
| 40 | + it 'should log error trace when new thread creation fails' do |
| 41 | + allow(Thread).to receive(:new).and_raise |
| 42 | + def some_callback(_args); end |
| 43 | + |
| 44 | + scheduler = Optimizely::AsyncScheduler.new(method(:some_callback), 10, false, spy_logger, error_handler) |
| 45 | + |
| 46 | + expect { scheduler.start! }.to raise_error(StandardError) |
| 47 | + expect(spy_logger).to have_received(:log).with( |
| 48 | + Logger::ERROR, |
| 49 | + "Couldn't create a new thread for async scheduler. " |
| 50 | + ) |
| 51 | + end |
37 | 52 | end
|
0 commit comments