Replace couchbase counters with drb version
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
require 'spec_helper'
|
||||
|
||||
|
||||
describe List do
|
||||
let(:supplier) { create :supplier }
|
||||
let(:supplier_password){'secret1'}
|
||||
let(:supplier) { create :supplier, email: 'supplier@qwaiter.com', password: supplier_password, confirmation_token: 'abc', confirmed_at: Time.now.utc, open: true }
|
||||
let(:user) { create :user }
|
||||
let(:section) { create :section, supplier: supplier}
|
||||
let(:table) { create :table, supplier: supplier}
|
||||
let(:list_options){ {supplier: supplier, table: table, section: section, user_ids: [user.id]} }
|
||||
let(:list){ create :list, list_options}
|
||||
|
||||
describe 'join requests' do
|
||||
describe '#send_table_join_request_for_user' do
|
||||
it "does not add an existing user to join_request_user_ids" do
|
||||
list.send_table_join_request_for_user! user
|
||||
expect(list.join_request_user_ids).not_to include user.id
|
||||
end
|
||||
it "adds a user to join_request_user_ids" do
|
||||
other_user = create :user
|
||||
list.send_table_join_request_for_user! other_user
|
||||
# test through persistance
|
||||
list.reload
|
||||
expect(list.join_request_user_ids).to eq [other_user.id]
|
||||
end
|
||||
|
||||
it "does not add a user multiple times" do
|
||||
other_user = create :user
|
||||
2.times { list.send_table_join_request_for_user! other_user }
|
||||
expect(list.join_request_user_ids).to eq [other_user.id]
|
||||
end
|
||||
|
||||
it "broadcasts it to the user" do
|
||||
other_user = create :user
|
||||
expect{
|
||||
list.send_table_join_request_for_user! other_user
|
||||
}.to broadcast_to_user(user.id).message('user_join_request').with(
|
||||
hash_including(:users, :join_request)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#approve_join_request_for_user" do
|
||||
let(:joining_user){ create :user }
|
||||
|
||||
before do
|
||||
list_options[:join_request_user_ids] = [joining_user.id]
|
||||
end
|
||||
|
||||
it "does not raise error when no user is associated" do
|
||||
unlinked_user = create :user
|
||||
expect{ list.approve_join_request_for_user!(unlinked_user) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "removes the user as wanting to join" do
|
||||
list.approve_join_request_for_user! joining_user
|
||||
list.join_request_user_ids.should_not include joining_user.id
|
||||
|
||||
# persistance check
|
||||
list.reload
|
||||
list.join_request_user_ids.should_not include joining_user.id
|
||||
end
|
||||
|
||||
it "sets the active list of the user to the specific list" do
|
||||
list.approve_join_request_for_user! joining_user
|
||||
joining_user.active_list_id.should == list.id
|
||||
|
||||
# persistance check
|
||||
joining_user.reload
|
||||
joining_user.active_list_id.should == list.id
|
||||
end
|
||||
|
||||
it "broadcasts the event to the user itself" do
|
||||
joining_user
|
||||
expect{ list.approve_join_request_for_user! joining_user }
|
||||
.to broadcast_to_user(joining_user).message('join_request_approved')
|
||||
.with( hash_including(:user) )
|
||||
end
|
||||
|
||||
it "broadcasts the event to other associated users" do
|
||||
expect{ list.approve_join_request_for_user! joining_user }
|
||||
.to broadcast_to_user(user).message('join_request_approved')
|
||||
.with( hash_including(:user) )
|
||||
end
|
||||
end
|
||||
|
||||
describe "#reject_join_request_for_user" do
|
||||
let(:joining_user){ create :user }
|
||||
|
||||
before do
|
||||
list_options[:join_request_user_ids] = [joining_user.id]
|
||||
end
|
||||
|
||||
it "does not raise error when no user is associated" do
|
||||
unlinked_user = create :user
|
||||
expect{ list.reject_join_request_for_user!(unlinked_user) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "removes the user as wanting to join" do
|
||||
list.reject_join_request_for_user! joining_user
|
||||
list.join_request_user_ids.should_not include joining_user.id
|
||||
|
||||
# persistance check
|
||||
list.reload
|
||||
list.join_request_user_ids.should_not include joining_user.id
|
||||
end
|
||||
|
||||
it "does not set the active list of the user to the specific list" do
|
||||
list.reject_join_request_for_user! joining_user
|
||||
joining_user.active_list_id.should_not be_present
|
||||
end
|
||||
|
||||
it "broadcasts the event to the user itself" do
|
||||
joining_user
|
||||
expect{ list.reject_join_request_for_user! joining_user }
|
||||
.to broadcast_to_user(joining_user).message('join_request_rejected')
|
||||
.with( id: "jr-#{joining_user.id}" )
|
||||
end
|
||||
|
||||
it "broadcasts the event to other associated users" do
|
||||
expect{ list.reject_join_request_for_user! joining_user }
|
||||
.to broadcast_to_user(user).message('join_request_rejected')
|
||||
.with( id: "jr-#{joining_user.id}" )
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -8,19 +8,12 @@ describe List do
|
||||
let(:user) { create :user }
|
||||
let(:section) { create :section, supplier: supplier}
|
||||
let(:table) { create :table, supplier: supplier}
|
||||
let(:list){ create :list, supplier: supplier, table: table, section: section, user_ids: [user.id] }
|
||||
let(:list_options){ {supplier: supplier, table: table, section: section, user_ids: [user.id]} }
|
||||
let(:list){ create :list, list_options}
|
||||
let(:product){ create :product, price: 2.22, supplier: supplier }
|
||||
let(:order) { create :order, user: user, list: list, supplier: supplier, section: section }
|
||||
let(:product_order ){ create :product_order, order: order, product: product, quantity: 3, price: 2.11 }
|
||||
subject { list }
|
||||
describe :as_json do
|
||||
it 'should include _id in as_json serialization' do
|
||||
list.as_json.keys.map(&:to_sym).should include :_id
|
||||
end
|
||||
it 'should include table_number in as_json serialization' do
|
||||
list.as_json.keys.should include :table_number
|
||||
end
|
||||
end
|
||||
|
||||
describe :mark_as_paid do
|
||||
it "should set paid_at to a time" do
|
||||
@@ -163,35 +156,4 @@ describe List do
|
||||
|
||||
describe 'product order creation'
|
||||
end
|
||||
|
||||
describe 'join requests' do
|
||||
describe '#send_table_join_request_for_user' do
|
||||
it "does not add an existing user to join_request_user_ids" do
|
||||
list.send_table_join_request_for_user! user
|
||||
expect(list.join_request_user_ids).not_to include user.id
|
||||
end
|
||||
it "adds a user to join_request_user_ids" do
|
||||
other_user = create :user
|
||||
list.send_table_join_request_for_user! other_user
|
||||
# test through persistance
|
||||
list.reload
|
||||
expect(list.join_request_user_ids).to eq [other_user.id]
|
||||
end
|
||||
|
||||
it "does not add a user multiple times" do
|
||||
other_user = create :user
|
||||
2.times { list.send_table_join_request_for_user! other_user }
|
||||
expect(list.join_request_user_ids).to eq [other_user.id]
|
||||
end
|
||||
|
||||
it "broadcasts it to the user" do
|
||||
other_user = create :user
|
||||
expect{
|
||||
list.send_table_join_request_for_user! other_user
|
||||
}.to broadcast_to_user(user.id).message('user_join_request').with(
|
||||
hash_including(:users, :join_request)
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,18 +16,18 @@ describe Supplier do
|
||||
supplier2.orders_placed_count.should == 3
|
||||
end
|
||||
|
||||
it 'cleans counter values if orders are no longer available' do
|
||||
old_connection = Qwaiter::Counter.connection
|
||||
# this spec should run on the couchbase database
|
||||
Qwaiter::Counter.connection = $cb
|
||||
supplier = create :supplier
|
||||
Qwaiter::Counter.set "supplier_counter:#{supplier.id}:orders_placed", 9
|
||||
supplier.orders_placed_count.should == 9
|
||||
Supplier.reset_counters!
|
||||
sleep 1
|
||||
supplier.orders_placed_count.should == 0
|
||||
Qwaiter::Counter.connection = old_connection
|
||||
end
|
||||
# it 'cleans counter values if orders are no longer available', broken: defined?($cb) do
|
||||
# old_connection = Qwaiter::Counter.connection
|
||||
# # this spec should run on the couchbase database
|
||||
# Qwaiter::Counter.connection = $cb
|
||||
# supplier = create :supplier
|
||||
# Qwaiter::Counter.set "supplier_counter:#{supplier.id}:orders_placed", 9
|
||||
# supplier.orders_placed_count.should == 9
|
||||
# Supplier.reset_counters!
|
||||
# sleep 1
|
||||
# supplier.orders_placed_count.should == 0
|
||||
# Qwaiter::Counter.connection = old_connection
|
||||
# end
|
||||
end
|
||||
|
||||
describe '#reset_counters!' do
|
||||
|
||||
+17
-10
@@ -2,11 +2,12 @@
|
||||
ENV["RAILS_ENV"] ||= 'test'
|
||||
require 'simplecov'
|
||||
SimpleCov.start 'rails'
|
||||
require File.expand_path("../../config/environment", __FILE__)
|
||||
require File.expand_path("../config/environment", File.dirname(__FILE__))
|
||||
require 'rspec/rails'
|
||||
require 'rspec/matchers'
|
||||
require 'capybara/rspec'
|
||||
require 'turnip/capybara'
|
||||
require 'in_memory_q_counter'
|
||||
|
||||
# Requires supporting ruby files with custom matchers and macros, etc,
|
||||
# in spec/support/ and its subdirectories.
|
||||
@@ -44,10 +45,20 @@ module SpecSelectorHelpers
|
||||
end
|
||||
end
|
||||
|
||||
class Couchbase::View
|
||||
alias :old_initialize :initialize
|
||||
def initialize(bucket, endpoint, params = {})
|
||||
old_initialize(bucket, endpoint, params.merge(stale: false))
|
||||
class TestCounter < InMemoryQCounter
|
||||
def incr(*args)
|
||||
result = super
|
||||
puts "Counter incr called with #{args.inspect} giving result #{result}"
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
if defined?(Couchbase)
|
||||
class Couchbase::View
|
||||
alias :old_initialize :initialize
|
||||
def initialize(bucket, endpoint, params = {})
|
||||
old_initialize(bucket, endpoint, params.merge(stale: false))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -108,11 +119,7 @@ RSpec.configure do |config|
|
||||
config.before :suite do
|
||||
Qwaiter::Couchbase.load_design_docs!
|
||||
# NOT THREADSAFE!!!!!! but good enough for testing since the real couchbase flush is slowwwwww....
|
||||
Qwaiter::Counter.connection = InMemoryQCounter.new
|
||||
# Threadsafe would be using the drb counter
|
||||
# require 'drb'
|
||||
# counter = DRbObject.new nil, 'druby://:9000'
|
||||
# Qwaiter::Counter.connection = counter
|
||||
Qwaiter::Counter.connection = TestCounter.new
|
||||
end
|
||||
|
||||
config.before :each do
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
# This is a non thread safe replacement for the
|
||||
# couchbase counter mechanism since every test needs
|
||||
# a clean start and Hash#clear is soooo much faster than
|
||||
# a couchbase bucket flush
|
||||
class InMemoryQCounter
|
||||
attr_reader :store
|
||||
|
||||
def initialize
|
||||
@store = {}
|
||||
end
|
||||
|
||||
def get(key, options = {})
|
||||
store[key]
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
store[key] = value
|
||||
end
|
||||
|
||||
def incr(key, options = {})
|
||||
# store[key] ||= options[:initial].to_i
|
||||
# store[key] += 1
|
||||
if store[key]
|
||||
store[key] += 1
|
||||
else
|
||||
store[key] = options[:initial].to_i
|
||||
end
|
||||
end
|
||||
|
||||
def decr(key, options = {})
|
||||
# store[key] ||= options[:initial].to_i
|
||||
# store[key] -= 1
|
||||
if store[key]
|
||||
store[key] -= 1
|
||||
else
|
||||
store[key] = options[:initial].to_i
|
||||
end
|
||||
end
|
||||
|
||||
def flush
|
||||
store.clear
|
||||
end
|
||||
end
|
||||
|
||||
=begin Make drb server
|
||||
require 'drb'
|
||||
DRb.start_service 'druby://:9000', InMemoryQCounter.new
|
||||
puts "Counter server running at #{DRb.uri}"
|
||||
trap("INT") { DRb.stop_service }
|
||||
DRb.thread.join
|
||||
=end
|
||||
@@ -14,6 +14,7 @@ module Matchers
|
||||
|
||||
def initialize(user_id)
|
||||
@user_id = user_id
|
||||
@user_id = @user_id.id if @user_id.is_a?(User)
|
||||
end
|
||||
|
||||
def matches?(block)
|
||||
@@ -24,8 +25,9 @@ module Matchers
|
||||
Qwaiter.broadcaster = old_broadcaster
|
||||
|
||||
relevant_broadcasts = test_broadcaster.broadcasts.select{|b| b[:channel] =~ /^\/user\/#{@user_id}/ && b[:data][:event] == @message}
|
||||
@failure_message = "User #{@user_id} did not receive any broadcasts" and return false if relevant_broadcasts.empty?
|
||||
@failure_debug_content = "was #{relevant_broadcasts.map{|b| b[:data][:data].inspect}.join(" and ")}"
|
||||
relevant_broadcasts.any?{|b| @target_object === b[:data][:data]}
|
||||
relevant_broadcasts.any? { |b| @target_object === b[:data][:data] }
|
||||
end
|
||||
|
||||
def message(msg)
|
||||
@@ -39,7 +41,7 @@ module Matchers
|
||||
end
|
||||
|
||||
def failure_message
|
||||
"user #{@user_id} did not receive broadcast #{@message} with #{@target_object.inspect} #{@failure_debug_content}"
|
||||
@failure_message || "user #{@user_id} did not receive broadcast #{@message} with #{@target_object.inspect} #{@failure_debug_content}"
|
||||
end
|
||||
def supports_block_expectations?; true; end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user