diff --git a/.drone.yml b/.drone.yml index 7c6bdaa..315111c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -1,22 +1,82 @@ -build: - test: - image: abakpress/dind-testing - pull: true - privileged: true - volumes: - - /home/data/drone/images:/images - - /home/data/drone/gems:/bundle +name: build + +kind: pipeline +type: docker + +volumes: + - name: rubygems + host: + path: /home/data/drone/rubygems + - name: images + host: + path: /home/data/drone/images + - name: bundle + host: + path: /home/data/drone/gems + - name: keys + host: + path: /home/data/drone/key_cache + +spec_step_common: &spec_step_common + image: abakpress/dind-testing:1.0.3 + pull: if-not-exists + privileged: true + volumes: + - name: images + path: /images + - name: bundle + path: /bundle + - name: keys + path: /ssh_keys + commands: + - prepare-build + + - fetch-images + --image whilp/ssh-agent + --image abakpress/ruby-app:$RUBY_IMAGE_TAG + --image redis:$REDIS_IMAGE_TAG + + - dip ssh add -T -v /ssh_keys -k /ssh_keys/id_rsa + - dip provision + - dip rspec + +steps: + - name: Tests Ruby 2.2 environment: - - COMPOSE_FILE_EXT=drone - - POSTGRES_IMAGE_TAG=9.3-latest - - SPHINX_IMAGE_TAG=2.2-latest - commands: - - wrapdocker docker -v + COMPOSE_FILE_EXT: drone + DOCKER_RUBY_VERSION: 2.2 + RUBY_IMAGE_TAG: 2.2-latest + REDIS_IMAGE_TAG: 4-alpine + RAILS_ENV: test + <<: *spec_step_common + + - name: Tests Ruby 2.3 + environment: + COMPOSE_FILE_EXT: drone + DOCKER_RUBY_VERSION: 2.3 + RUBY_IMAGE_TAG: 2.3-latest + REDIS_IMAGE_TAG: 4-alpine + RAILS_ENV: test + <<: *spec_step_common - - fetch-images - --image abakpress/ruby-app:$RUBY_IMAGE_TAG - --image abakpress/postgres-db:$POSTGRES_IMAGE_TAG - --image abakpress/sphinx-index:$SPHINX_IMAGE_TAG + - name: Tests Ruby 2.4 + environment: + COMPOSE_FILE_EXT: drone + DOCKER_RUBY_VERSION: 2.4 + RUBY_IMAGE_TAG: 2.4-latest + REDIS_IMAGE_TAG: 4-alpine + RAILS_ENV: test + <<: *spec_step_common - - dip provision - - dip rspec + - name: release + image: abakpress/gem-publication:latest + pull: if-not-exists + when: + event: push + branch: master + status: success + volumes: + - name: rubygems + path: /root/.gem + commands: + - release-gem --public diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..dd00f00 --- /dev/null +++ b/Appraisals @@ -0,0 +1,9 @@ +if RUBY_VERSION < '2.4' + appraise 'rails4.0' do + gem 'activesupport', '~> 4.0.0' + end +end + +appraise 'rails4.2' do + gem 'activesupport', '~> 4.2.0' +end diff --git a/dip.yml b/dip.yml index be2111f..2c7566b 100644 --- a/dip.yml +++ b/dip.yml @@ -3,6 +3,7 @@ version: '1' environment: DOCKER_RUBY_VERSION: 2.2 RUBY_IMAGE_TAG: 2.2-latest + REDIS_IMAGE_TAG: 4-alpine COMPOSE_FILE_EXT: development RAILS_ENV: test diff --git a/docker-compose.yml b/docker-compose.yml index 6878f3a..2189a10 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,4 +6,11 @@ services: environment: - BUNDLE_PATH=/bundle/$DOCKER_RUBY_VERSION - BUNDLE_CONFIG=/app/.bundle/config + - TEST_REDIS_HOST=redis command: bash + depends_on: + - redis + + redis: + image: redis:$REDIS_IMAGE_TAG + command: 'redis-server --bind 0.0.0.0' diff --git a/redis_counters.gemspec b/redis_counters.gemspec index 50fbab3..dd9f0f7 100644 --- a/redis_counters.gemspec +++ b/redis_counters.gemspec @@ -1,4 +1,3 @@ -# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'redis_counters/version' @@ -16,12 +15,13 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ['lib'] - spec.add_dependency 'activesupport', '>= 3.0', '< 5' + spec.add_runtime_dependency 'activesupport', '>= 4.0', '< 5' spec.add_development_dependency 'bundler' spec.add_development_dependency 'rake' spec.add_development_dependency 'rspec', '~> 2.14.0' - spec.add_development_dependency 'mock_redis' + spec.add_development_dependency 'redis', '>= 3.0' + spec.add_development_dependency 'appraisal', '>= 1.0.2' spec.add_development_dependency 'timecop' spec.add_development_dependency 'codeclimate-test-reporter', '>= 0.4.1' spec.add_development_dependency 'simplecov' diff --git a/spec/redis_counters/base_spec.rb b/spec/redis_counters/base_spec.rb index 8b56ce3..dce480d 100644 --- a/spec/redis_counters/base_spec.rb +++ b/spec/redis_counters/base_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe RedisCounters::BaseCounter do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:options) { { :counter_class => RedisCounters::HashCounter, @@ -26,4 +26,4 @@ it { expect { counter }.to raise_error KeyError } end -end \ No newline at end of file +end diff --git a/spec/redis_counters/hash_counter_spec.rb b/spec/redis_counters/hash_counter_spec.rb index 1df7aae..ed7d9e8 100644 --- a/spec/redis_counters/hash_counter_spec.rb +++ b/spec/redis_counters/hash_counter_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe RedisCounters::HashCounter do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:value) { rand(10) + 1 } let(:options) { { :counter_name => :test_counter, :field_name => :test_field } } let(:counter) { described_class.new(redis, options) } @@ -89,8 +89,7 @@ before { 3.times { counter.process(:param1 => 21, :param2 => 22, :param3 => 33) } } it { expect(redis.keys('*')).to have(2).key } - it { expect(redis.keys('*').first).to eq 'test_counter:11:22' } - it { expect(redis.keys('*').last).to eq 'test_counter:21:22' } + it { expect(redis.keys('*')).to match_array ['test_counter:11:22', 'test_counter:21:22'] } it { expect(redis.hexists('test_counter:11:22', 'test_field')).to be_true } it { expect(redis.hget('test_counter:11:22', 'test_field')).to eq value.to_s } it { expect(redis.hexists('test_counter:21:22', 'test_field')).to be_true } @@ -108,8 +107,7 @@ before { 3.times { counter.process(:param1 => 2, :param2 => 2) } } it { expect(redis.keys('*')).to have(2).key } - it { expect(redis.keys('*').first).to eq 'test_counter:true' } - it { expect(redis.keys('*').last).to eq 'test_counter:false' } + it { expect(redis.keys('*')).to match_array ['test_counter:true', 'test_counter:false'] } it { expect(redis.hexists('test_counter:true', 'test_field')).to be_true } it { expect(redis.hget('test_counter:true', 'test_field')).to eq 2.to_s } it { expect(redis.hexists('test_counter:false', 'test_field')).to be_true } @@ -129,9 +127,7 @@ before { 1.times { counter.process(:param1 => 21, :param2 => '', :param3 => 33) } } it { expect(redis.keys('*')).to have(3).key } - it { expect(redis.keys('*').first).to eq 'test_counter:11:22' } - it { expect(redis.keys('*').second).to eq 'test_counter:21:22' } - it { expect(redis.keys('*').third).to eq 'test_counter:21:' } + it { expect(redis.keys('*')).to match_array ['test_counter:11:22', 'test_counter:21:22', 'test_counter:21:'] } it { expect(redis.hexists('test_counter:11:22', 'test_field')).to be_true } it { expect(redis.hget('test_counter:11:22', 'test_field')).to eq value.to_s } it { expect(redis.hexists('test_counter:21:22', 'test_field')).to be_true } @@ -153,9 +149,13 @@ before { 1.times { counter.process(:param1 => 2, :param2 => 2, :date => '2013-04-28') } } it { expect(redis.keys('*')).to have(3).key } - it { expect(redis.keys('*').first).to eq 'test_counter:2013-04-27:true' } - it { expect(redis.keys('*').second).to eq 'test_counter:2013-04-27:false' } - it { expect(redis.keys('*').third).to eq 'test_counter:2013-04-28:false' } + it do + expect(redis.keys('*')).to match_array [ + 'test_counter:2013-04-27:true', + 'test_counter:2013-04-27:false', + 'test_counter:2013-04-28:false' + ] + end it { expect(redis.hexists('test_counter:2013-04-27:true', 'test_field')).to be_true } it { expect(redis.hget('test_counter:2013-04-27:true', 'test_field')).to eq 3.to_s } it { expect(redis.hexists('test_counter:2013-04-27:false', 'test_field')).to be_true } @@ -182,17 +182,14 @@ context 'when no partition params given' do it { expect(counter.partitions).to have(3).partition } - it { expect(counter.partitions.first).to eq partition_1 } - it { expect(counter.partitions.second).to eq partition_2 } - it { expect(counter.partitions.third).to eq partition_3 } + it { expect(counter.partitions).to match_array [partition_1, partition_2, partition_3] } end context 'when one partition param given' do it { expect(counter.partitions(:param1 => 11)).to have(1).partition } it { expect(counter.partitions(:param1 => 11).first).to eq partition_1 } it { expect(counter.partitions(:param1 => 21)).to have(2).partition } - it { expect(counter.partitions(:param1 => 21).first).to eq partition_2 } - it { expect(counter.partitions('param1' => 21).second).to eq partition_3 } + it { expect(counter.partitions(:param1 => 21)).to match_array [partition_2, partition_3] } end context 'when two partition params given' do @@ -231,9 +228,7 @@ before { 4.times { counter.process(:param1 => 21, :param2 => '', :param3 => 31) } } it { expect(counter.data(partitions)).to have(3).row } - it { expect(counter.data(partitions).first[:value]).to eq 3 } - it { expect(counter.data(partitions).second[:value]).to eq 2 } - it { expect(counter.data(partitions).third[:value]).to eq 5 } + it { expect(counter.data(partitions)).to match_array [{'value' => 3}, {'value' => 2}, {'value' => 5}] } end context 'when group_keys and one group key is nil' do @@ -375,8 +370,7 @@ before { counter.delete_partitions!(partition_2) } it { expect(counter.partitions).to have(2).row } - it { expect(counter.partitions.first).to eq partition_1 } - it { expect(counter.partitions.last).to eq partition_3 } + it { expect(counter.partitions).to match_array [partition_1, partition_3] } end context 'when not leaf partition given' do @@ -396,9 +390,7 @@ before { counter.delete_partitions!(:param1 => 21, &error_proc) rescue nil } it { expect(counter.partitions).to have(3).row } - it { expect(counter.partitions.first).to eq partition_1 } - it { expect(counter.partitions.second).to eq partition_2 } - it { expect(counter.partitions.last).to eq partition_3 } + it { expect(counter.partitions).to match_array [partition_1, partition_2, partition_3] } end end @@ -407,8 +399,7 @@ before { counter.delete_partitions!(partition_2) } it { expect(counter.partitions).to have(2).row } - it { expect(counter.partitions.first).to eq partition_1 } - it { expect(counter.partitions.last).to eq partition_3 } + it { expect(counter.partitions).to match_array [partition_1, partition_3] } end context 'when not leaf partition given' do @@ -434,16 +425,21 @@ before { 2.times { counter.process(partition_2) } } before { 3.times { counter.process(partition_3) } } - it { expect(counter.partitions.first[:param2]).to eq '22:35' } - it { expect(counter.partitions.second[:param2]).to eq '23:26' } - it { expect(counter.partitions.third[:param2]).to eq '24:26' } + it do + expect(counter.partitions).to match_array [ + {'param1' => '11', 'param2' => '22:35'}, + {'param1' => '11', 'param2' => '23:26'}, + {'param1' => '11', 'param2' => '24:26'} + ] + end - it { expect(counter.data.first[:param3]).to eq '11:64' } - it { expect(counter.data.first[:value]).to eq 1 } - it { expect(counter.data.second[:param3]).to eq '11:36' } - it { expect(counter.data.second[:value]).to eq 2 } - it { expect(counter.data.third[:param3]).to eq '21:54' } - it { expect(counter.data.third[:value]).to eq 3 } + it do + expect(counter.data).to match_array [ + {'value' => 1, 'param3' => '11:64'}, + {'value' => 2, 'param3' => '11:36'}, + {'value' => 3, 'param3' => '21:54'} + ] + end end context 'two delimiters' do diff --git a/spec/redis_counters/unique_hash_counter_spec.rb b/spec/redis_counters/unique_hash_counter_spec.rb index 3f8248a..7f2dc57 100644 --- a/spec/redis_counters/unique_hash_counter_spec.rb +++ b/spec/redis_counters/unique_hash_counter_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe RedisCounters::UniqueHashCounter do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:unique_list_postfix) { described_class.const_get(:UNIQUE_LIST_POSTFIX) } let(:options) { { @@ -76,8 +76,8 @@ it { expect(redis.lrange('test_counter_uq:1:partitions', 0, -1)).to eq ['2013-04-28', '2013-04-27'] } it { expect(redis.lrange('test_counter_uq:2:partitions', 0, -1)).to eq ['2013-04-27'] } - it { expect(redis.smembers('test_counter_uq:1:2013-04-27')).to eq ['4'] } - it { expect(redis.smembers('test_counter_uq:2:2013-04-27')).to eq ['3', '2', '1'] } - it { expect(redis.smembers('test_counter_uq:1:2013-04-28')).to eq ['5', '1'] } + it { expect(redis.smembers('test_counter_uq:1:2013-04-27')).to match_array ['4'] } + it { expect(redis.smembers('test_counter_uq:2:2013-04-27')).to match_array ['3', '2', '1'] } + it { expect(redis.smembers('test_counter_uq:1:2013-04-28')).to match_array ['5', '1'] } end -end \ No newline at end of file +end diff --git a/spec/redis_counters/unique_values_lists/blocking_spec.rb b/spec/redis_counters/unique_values_lists/blocking_spec.rb index 77edff9..873d69b 100644 --- a/spec/redis_counters/unique_values_lists/blocking_spec.rb +++ b/spec/redis_counters/unique_values_lists/blocking_spec.rb @@ -5,7 +5,7 @@ it_behaves_like 'unique_values_lists/set' context 'when check partitions list' do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:values) { rand(10) + 1 } let(:partitions_list_postfix) { described_class.const_get(:PARTITIONS_LIST_POSTFIX) } @@ -91,4 +91,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f3dfa2c..b8faee8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,5 @@ require 'bundler/setup' require 'rspec' -require 'mock_redis' require 'timecop' require 'codeclimate-test-reporter' require 'simplecov' @@ -9,6 +8,9 @@ SimpleCov.start('test_frameworks') require 'redis_counters' +require 'redis' + +Redis.current = Redis.new(host: ENV['TEST_REDIS_HOST']) # require helpers support_dir = File.expand_path(File.join('..', 'support'), __FILE__) @@ -16,4 +18,5 @@ RSpec.configure do |config| config.backtrace_exclusion_patterns = [/lib\/rspec\/(core|expectations|matchers|mocks)/] + config.before { Redis.current.flushdb } end diff --git a/spec/support/unique_values_lists/common.rb b/spec/support/unique_values_lists/common.rb index c870ca7..eb65e0a 100644 --- a/spec/support/unique_values_lists/common.rb +++ b/spec/support/unique_values_lists/common.rb @@ -1,6 +1,5 @@ -# coding: utf-8 shared_examples_for 'unique_values_lists/common' do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:values) { rand(10) + 1 } let(:options) { { @@ -139,9 +138,10 @@ context 'when no partition given' do it { expect(counter.partitions(cluster1_subcluster1)).to have(3).partitions } - it { expect(counter.partitions(cluster1_subcluster1).first).to eq part1_subpart1 } - it { expect(counter.partitions(cluster1_subcluster1).second).to eq part1_subpart2 } - it { expect(counter.partitions(cluster1_subcluster1).third).to eq part2_subpart1 } + it do + expect(counter.partitions(cluster1_subcluster1)). + to match_array [part1_subpart1, part1_subpart2, part2_subpart1] + end # it { expect(counter.partitions(cluster2_subcluster1)).to have(1).partitions } it { expect(counter.partitions(cluster2_subcluster1).first).to eq part1_subpart1 } @@ -149,8 +149,10 @@ context 'when not leaf partition given' do it { expect(counter.partitions(cluster1_subcluster1.merge(:part => :part1))).to have(2).partitions } - it { expect(counter.partitions(cluster1_subcluster1.merge(:part => :part1)).first).to eq part1_subpart1 } - it { expect(counter.partitions(cluster1_subcluster1.merge(:part => :part1)).second).to eq part1_subpart2 } + it do + expect(counter.partitions(cluster1_subcluster1.merge(:part => :part1))). + to match_array [part1_subpart1, part1_subpart2] + end end context 'when leaf partition given' do @@ -182,15 +184,12 @@ context 'when no partition given' do it { expect(counter.partitions).to have(3).partitions } - it { expect(counter.partitions.first).to eq part1_subpart1 } - it { expect(counter.partitions.second).to eq part1_subpart2 } - it { expect(counter.partitions.third).to eq part2_subpart1 } + it { expect(counter.partitions).to match_array [part1_subpart1, part1_subpart2, part2_subpart1] } end context 'when not leaf partition given' do it { expect(counter.partitions(:part => :part1)).to have(2).partitions } - it { expect(counter.partitions(:part => :part1).first).to eq part1_subpart1 } - it { expect(counter.partitions(:part => :part1).second).to eq part1_subpart2 } + it { expect(counter.partitions(:part => :part1)).to match_array [part1_subpart1, part1_subpart2] } end context 'when leaf partition given' do @@ -560,4 +559,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/support/unique_values_lists/expirable.rb b/spec/support/unique_values_lists/expirable.rb index 388c5d9..bed03d1 100644 --- a/spec/support/unique_values_lists/expirable.rb +++ b/spec/support/unique_values_lists/expirable.rb @@ -1,6 +1,5 @@ -# coding: utf-8 shared_examples_for 'unique_values_lists/expirable' do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:counter) { described_class.new(redis, options) } after do @@ -159,4 +158,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/support/unique_values_lists/set.rb b/spec/support/unique_values_lists/set.rb index f5c7331..cb29453 100644 --- a/spec/support/unique_values_lists/set.rb +++ b/spec/support/unique_values_lists/set.rb @@ -1,6 +1,5 @@ -# coding: utf-8 shared_examples_for 'unique_values_lists/set' do - let(:redis) { MockRedis.new } + let(:redis) { Redis.current } let(:values) { rand(10) + 1 } let(:counter) { described_class.new(redis, options) } @@ -116,4 +115,4 @@ it { expect(redis.smembers("test_counter:cluster2")).to include '4:5' } end end -end \ No newline at end of file +end