cellect-server 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +2 -8
  4. data/cellect-server.gemspec +4 -4
  5. data/cellect.gemspec +4 -3
  6. data/lib/cellect.rb +1 -1
  7. data/lib/cellect/node_set.rb +12 -12
  8. data/lib/cellect/server.rb +4 -4
  9. data/lib/cellect/server/adapters.rb +2 -2
  10. data/lib/cellect/server/adapters/default.rb +6 -6
  11. data/lib/cellect/server/adapters/postgres.rb +5 -5
  12. data/lib/cellect/server/api.rb +9 -9
  13. data/lib/cellect/server/api/helpers.rb +5 -5
  14. data/lib/cellect/server/api/sets.rb +2 -2
  15. data/lib/cellect/server/api/users.rb +5 -5
  16. data/lib/cellect/server/grouped_workflow.rb +10 -10
  17. data/lib/cellect/server/node_set.rb +2 -2
  18. data/lib/cellect/server/user.rb +10 -10
  19. data/lib/cellect/server/workflow.rb +21 -21
  20. data/lib/cellect/version.rb +1 -1
  21. data/spec/server/api/add_seen_spec.rb +2 -2
  22. data/spec/server/api/add_spec.rb +4 -4
  23. data/spec/server/api/remove_spec.rb +4 -4
  24. data/spec/server/api/sample_spec.rb +3 -3
  25. data/spec/server/api/user_load_spec.rb +2 -2
  26. data/spec/server/grouped_workflow_spec.rb +10 -10
  27. data/spec/server/node_set_spec.rb +1 -1
  28. data/spec/server/server_spec.rb +2 -2
  29. data/spec/server/user_spec.rb +5 -5
  30. data/spec/server/workflow_spec.rb +9 -9
  31. data/spec/spec_helper.rb +3 -4
  32. data/spec/support/shared_api_context.rb +2 -2
  33. data/spec/support/shared_examples_for_node_set.rb +3 -3
  34. data/spec/support/shared_examples_for_set.rb +6 -6
  35. data/spec/support/shared_examples_for_workflow.rb +5 -5
  36. data/spec/support/spec_adapter.rb +6 -6
  37. data/spec/support/zk_setup.rb +48 -25
  38. metadata +3 -3
@@ -4,13 +4,13 @@ module Cellect::Server
4
4
  describe Cellect do
5
5
  context 'default adapter' do
6
6
  let(:default){ Cellect::Server::Adapters::Default.new }
7
-
7
+
8
8
  it 'should raise a NotImplementedError when using the default adapter' do
9
9
  expect{ default.workflow_list }.to raise_error NotImplementedError
10
10
  expect{ default.load_data_for(Workflow.new('test')) }.to raise_error NotImplementedError
11
11
  expect{ default.load_user 'random', 123 }.to raise_error NotImplementedError
12
12
  end
13
-
13
+
14
14
  it 'should return a workflow given a set of options' do
15
15
  expect(default.workflow_for('name' => 'a')).to be_an_instance_of Workflow
16
16
  expect(default.workflow_for('name' => 'b', 'grouped' => true)).to be_an_instance_of GroupedWorkflow
@@ -3,24 +3,24 @@ require 'spec_helper'
3
3
  module Cellect::Server
4
4
  describe User do
5
5
  let(:user){ User.new 1, workflow_name: 'random' }
6
-
6
+
7
7
  it 'should store seen ids' do
8
8
  expect(user.seen).to be_a DiffSet::RandomSet
9
9
  end
10
-
10
+
11
11
  it 'should have a default ttl of 15 minutes' do
12
12
  expect(user.ttl).to eq 900 # seconds
13
13
  end
14
-
14
+
15
15
  it 'should allow custom ttl' do
16
16
  expect(User.new(2, workflow_name: 'random', ttl: 123).ttl).to eq 123
17
17
  end
18
-
18
+
19
19
  it 'should reset the ttl timer on activity' do
20
20
  expect(user.bare_object).to receive(:restart_ttl_timer).at_least :once
21
21
  user.seen
22
22
  end
23
-
23
+
24
24
  it 'should terminate on ttl expiry' do
25
25
  async_workflow = double
26
26
  expect(Workflow[user.workflow_name]).to receive(:async).and_return async_workflow
@@ -6,29 +6,29 @@ module Cellect::Server
6
6
  expect(Cellect::Server.adapter).to receive(:load_workflows).with('random').and_call_original
7
7
  Workflow['random']
8
8
  end
9
-
9
+
10
10
  SET_TYPES.each do |workflow_type|
11
11
  context workflow_type do
12
12
  it_behaves_like 'workflow', :workflow
13
13
  let(:workflow){ Workflow[workflow_type] }
14
14
  let(:user){ workflow.user 123 }
15
15
  before(:each){ pass_until workflow, is: :ready }
16
-
16
+
17
17
  it 'should provide unseen for users' do
18
18
  expect(workflow.subjects).to receive(:subtract).with user.seen, 3
19
19
  workflow.unseen_for 123, limit: 3
20
20
  end
21
-
21
+
22
22
  it 'should sample subjects without a user' do
23
23
  expect(workflow.subjects).to receive(:sample).with 3
24
24
  workflow.sample limit: 3
25
25
  end
26
-
26
+
27
27
  it 'should sample subjects with a user' do
28
28
  expect(workflow.subjects).to receive(:subtract).with user.seen, 3
29
29
  workflow.sample user_id: 123, limit: 3
30
30
  end
31
-
31
+
32
32
  it 'should add subjects' do
33
33
  if workflow.prioritized?
34
34
  expect(workflow.subjects).to receive(:add).with 123, 456
@@ -38,26 +38,26 @@ module Cellect::Server
38
38
  workflow.add subject_id: 123
39
39
  end
40
40
  end
41
-
41
+
42
42
  it 'should remove subjects' do
43
43
  expect(workflow.subjects).to receive(:remove).with 123
44
44
  workflow.remove subject_id: 123
45
45
  end
46
-
46
+
47
47
  it 'should be notified of a user ttl expiry' do
48
48
  async_workflow = double
49
49
  expect(workflow).to receive(:async).and_return async_workflow
50
50
  expect(async_workflow).to receive(:remove_user).with user.id
51
51
  user.ttl_expired!
52
52
  end
53
-
53
+
54
54
  it 'should remove users when their ttl expires' do
55
55
  id = user.id
56
56
  workflow.remove_user id
57
57
  expect(workflow.users).to_not have_key id
58
58
  expect{ user.id }.to raise_error Celluloid::DeadActorError
59
59
  end
60
-
60
+
61
61
  it 'should not be grouped' do
62
62
  expect(workflow).to_not be_grouped
63
63
  end
data/spec/spec_helper.rb CHANGED
@@ -12,7 +12,6 @@ Bundler.require :test, :development
12
12
 
13
13
  ENV['CELLECT_POOL_SIZE'] = '3'
14
14
  SPAWN_ZK = !ENV['ZK_URL']
15
-
16
15
  require 'pry'
17
16
  require 'oj'
18
17
  require './spec/support/zk_setup.rb'
@@ -32,14 +31,14 @@ RSpec.configure do |config|
32
31
  config.filter_run :focus
33
32
  config.order = 'random'
34
33
  config.include CellectHelper
35
-
34
+
36
35
  config.around(:each) do |example|
37
36
  Celluloid.boot
38
37
  example.run
39
38
  Celluloid.shutdown
40
39
  end
41
-
40
+
42
41
  config.after(:suite) do
43
- `zkServer stop #{ CELLECT_ZK_CONFIG } > /dev/null 2>&1` if SPAWN_ZK
42
+ ZK_Setup.stop_zk
44
43
  end
45
44
  end
@@ -1,10 +1,10 @@
1
1
  shared_context 'API' do
2
2
  include Rack::Test::Methods
3
-
3
+
4
4
  def app
5
5
  $app ||= Cellect::Server::API.new
6
6
  end
7
-
7
+
8
8
  def json
9
9
  Oj.strict_load last_response.body
10
10
  end
@@ -1,18 +1,18 @@
1
1
  shared_examples_for 'node set' do
2
2
  let(:node_set){ Cellect::NodeSet.new }
3
-
3
+
4
4
  it 'should connect to zoo keeper' do
5
5
  expect(node_set.zk).to be_nil
6
6
  pass_until node_set, is: :ready
7
7
  expect(node_set.zk).to be_connected
8
8
  end
9
-
9
+
10
10
  it 'should know the connection state' do
11
11
  expect(node_set.state).to be :initializing
12
12
  pass_until node_set, is: :ready
13
13
  expect(node_set).to be_ready
14
14
  end
15
-
15
+
16
16
  it 'should accept a connection string' do
17
17
  url_before = ENV['ZK_URL']
18
18
  begin
@@ -2,30 +2,30 @@ shared_examples_for 'a set' do
2
2
  it 'should convert to an Array' do
3
3
  expect(set.to_a).to eq (1..5).to_a
4
4
  end
5
-
5
+
6
6
  it 'should add elements' do
7
7
  set.add 100
8
8
  expect(set.to_a).to include 100
9
9
  end
10
-
10
+
11
11
  it 'should remove elements' do
12
12
  set.remove 1
13
13
  set.to_a.should_not include 1
14
14
  end
15
-
15
+
16
16
  it 'should sample elements' do
17
17
  expect(set.sample(2).length).to eq 2
18
18
  end
19
-
19
+
20
20
  it 'should not include removed elements in samples' do
21
21
  set.remove 5
22
22
  set.sample(5).should_not include 5
23
23
  end
24
-
24
+
25
25
  it 'should know how many elements it contains' do
26
26
  expect{ set.add 100 }.to change{ set.size }.from(5).to 6
27
27
  end
28
-
28
+
29
29
  it 'should know if it contains an element' do
30
30
  set.should_not include 100
31
31
  set.add 100
@@ -1,23 +1,23 @@
1
1
  shared_examples_for 'workflow' do |name|
2
2
  let(:obj){ send name }
3
-
3
+
4
4
  before(:each) do
5
5
  Cellect::Server.adapter.load_workflow obj.name
6
6
  end
7
-
7
+
8
8
  it 'should add singleton instances to the registry' do
9
9
  expect(obj.class[obj.name]).to be_a_kind_of Cellect::Server::Workflow
10
10
  expect(obj.class[obj.name].object_id).to eq obj.class[obj.name].object_id
11
11
  end
12
-
12
+
13
13
  it 'should initialize empty' do
14
14
  expect(obj.name).to be_a String
15
15
  expect(obj.users).to be_a Hash
16
-
16
+
17
17
  set_klass = obj.prioritized? ? DiffSet::PrioritySet : DiffSet::RandomSet
18
18
  expect(obj.subjects).to be_a set_klass
19
19
  end
20
-
20
+
21
21
  it 'should provide a user lookup' do
22
22
  expect(obj.user(1)).to be_a Cellect::Server::User
23
23
  expect(obj.user(1).object_id).to eq obj.user(1).object_id
@@ -8,11 +8,11 @@ class SpecAdapter < Cellect::Server::Adapters::Default
8
8
  fixtures.values_at(*names)
9
9
  end
10
10
  end
11
-
11
+
12
12
  def load_data_for(workflow_name)
13
13
  fixtures.fetch(workflow_name, { }).fetch 'entries', []
14
14
  end
15
-
15
+
16
16
  def fixtures
17
17
  @fixtures ||= { }.tap do |fixtures|
18
18
  Dir["#{ _fixture_path }/workflow_data/*.json"].collect do |f|
@@ -22,7 +22,7 @@ class SpecAdapter < Cellect::Server::Adapters::Default
22
22
  end
23
23
  end
24
24
  end
25
-
25
+
26
26
  def user_fixtures
27
27
  @user_fixtures ||= { }.tap do |user_fixtures|
28
28
  Dir["#{ _fixture_path }/user_data/*.json"].sort.collect.with_index do |f, i|
@@ -33,14 +33,14 @@ class SpecAdapter < Cellect::Server::Adapters::Default
33
33
  end
34
34
  end
35
35
  end
36
-
36
+
37
37
  def load_user(workflow_name, id)
38
38
  user = user_fixtures[id]
39
39
  user ? user[workflow_name] : user_fixtures['new_user'][workflow_name]
40
40
  end
41
-
41
+
42
42
  protected
43
-
43
+
44
44
  def _fixture_path
45
45
  File.expand_path File.join(__FILE__, '../../fixtures')
46
46
  end
@@ -1,28 +1,51 @@
1
- if SPAWN_ZK
2
- zk_dir = File.join CELLECT_ROOT, 'tmp/zookeeper'
3
- CELLECT_ZK_CONFIG = "#{ zk_dir }/zoo.cfg"
4
-
5
- `rm -rf #{ zk_dir }; mkdir -p #{ zk_dir }`
6
-
7
- File.open(CELLECT_ZK_CONFIG, 'w') do |out|
8
- out.puts <<-TEXT
9
- tickTime=2000
10
- initLimit=10
11
- syncLimit=5
12
- dataDir=#{ zk_dir }
13
- clientPort=21811
14
- forceSync=no
15
- snapCount=1000000
16
- TEXT
1
+ module ZK_Setup
2
+ def self.start_zk(port=21811)
3
+ @port = port
4
+ if SPAWN_ZK
5
+ kill_old_zk_servers
6
+ remove_zk_data
7
+ server = ZK::Server.new do |config|
8
+ config.client_port = port
9
+ config.client_port_address = 'localhost'
10
+ config.force_sync = false
11
+ config.tick_time = 2000
12
+ config.init_limit = 10
13
+ config.sync_limit = 5
14
+ config.snap_count = 1000000
15
+ config.base_dir = zk_dir
16
+ end
17
+ server.run
18
+ @zk_server = server
19
+ ENV['ZK_URL'] = "localhost:#{port}"
20
+ end
17
21
  end
18
-
19
- if `echo ruok | nc 127.0.0.1 21811`.chomp == 'imok'
20
- pid = `ps aux | grep -e 'Cellect[\/]tmp[\/]zookeeper'`.split[1]
21
- puts "Killing rogue zookeeper process: #{ pid }..."
22
- `kill -s TERM #{ pid }`
23
- sleep 1
22
+
23
+ def self.stop_zk
24
+ if SPAWN_ZK
25
+ @zk_server.shutdown
26
+ end
27
+ end
28
+
29
+ def self.zk_dir
30
+ File.join CELLECT_ROOT, 'tmp/zookeeper'
31
+ end
32
+
33
+ def self.zk_ok?
34
+ `echo ruok | nc 127.0.0.1 #{@port}`.chomp == 'imok'
35
+ end
36
+
37
+ def self.kill_old_zk_servers
38
+ if zk_ok?
39
+ pid = `ps aux | grep -e 'Cellect[\/]tmp[\/]zookeeper'`.split[1]
40
+ puts "Killing rogue zookeeper process: #{ pid }..."
41
+ `kill -s TERM #{ pid }`
42
+ sleep 1
43
+ end
44
+ end
45
+
46
+ def self.remove_zk_data
47
+ `rm -rf #{ zk_dir }; mkdir -p #{ zk_dir }`
24
48
  end
25
-
26
- `zkServer start #{ CELLECT_ZK_CONFIG } > /dev/null 2>&1`
27
- ENV['ZK_URL'] = 'localhost:21811'
28
49
  end
50
+
51
+ ZK_Setup.start_zk
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cellect-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Parrish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-24 00:00:00.000000000 Z
11
+ date: 2015-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -304,7 +304,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
304
304
  version: '0'
305
305
  requirements: []
306
306
  rubyforge_project:
307
- rubygems_version: 2.5.0
307
+ rubygems_version: 2.4.2
308
308
  signing_key:
309
309
  specification_version: 4
310
310
  summary: ''