kazoo-ruby 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/kazoo.rb +7 -0
- data/lib/kazoo/broker.rb +31 -16
- data/lib/kazoo/cli/consumers.rb +36 -1
- data/lib/kazoo/cli/topics.rb +14 -1
- data/lib/kazoo/cluster.rb +61 -29
- data/lib/kazoo/consumergroup.rb +148 -34
- data/lib/kazoo/partition.rb +9 -4
- data/lib/kazoo/replica_assigner.rb +74 -0
- data/lib/kazoo/subscription.rb +215 -0
- data/lib/kazoo/topic.rb +128 -82
- data/lib/kazoo/version.rb +1 -1
- data/test/functional/functional_consumergroup_test.rb +105 -53
- data/test/functional/functional_subscription_test.rb +62 -0
- data/test/functional/functional_topic_management_test.rb +25 -2
- data/test/replica_assigner_test.rb +80 -0
- data/test/subscription_test.rb +148 -0
- data/test/topic_test.rb +6 -19
- metadata +10 -2
data/lib/kazoo/version.rb
CHANGED
@@ -7,11 +7,18 @@ class FunctionalConsumergroupTest < Minitest::Test
|
|
7
7
|
@cg = Kazoo::Consumergroup.new(@cluster, 'test.kazoo')
|
8
8
|
@cg.create
|
9
9
|
|
10
|
-
@
|
10
|
+
@topic1 = @cluster.topic('test.1')
|
11
|
+
@topic1.destroy if @topic1.exists?
|
12
|
+
@topic1 = @cluster.create_topic('test.1', partitions: 1, replication_factor: 1)
|
13
|
+
|
14
|
+
@topic4 = @cluster.topic('test.4')
|
15
|
+
@topic4.destroy if @topic4.exists?
|
16
|
+
@topic4 = @cluster.create_topic('test.4', partitions: 4, replication_factor: 1)
|
11
17
|
end
|
12
18
|
|
13
19
|
def teardown
|
14
|
-
@
|
20
|
+
@topic1.destroy if @topic1.exists?
|
21
|
+
@topic4.destroy if @topic4.exists?
|
15
22
|
|
16
23
|
cg = Kazoo::Consumergroup.new(@cluster, 'test.kazoo')
|
17
24
|
cg.destroy if cg.exists?
|
@@ -30,32 +37,19 @@ class FunctionalConsumergroupTest < Minitest::Test
|
|
30
37
|
refute cg.exists?
|
31
38
|
end
|
32
39
|
|
33
|
-
def test_retrieve_and_commit_offsets
|
34
|
-
topic = Kazoo::Topic.new(@cluster, 'test.1')
|
35
|
-
partition = Kazoo::Partition.new(topic, 0)
|
36
|
-
|
37
|
-
assert_nil @cg.retrieve_offset(partition)
|
38
|
-
|
39
|
-
@cg.commit_offset(partition, 1234)
|
40
|
-
|
41
|
-
assert_equal 1234 + 1, @cg.retrieve_offset(partition)
|
42
|
-
|
43
|
-
@cg.reset_all_offsets
|
44
|
-
assert_nil @cg.retrieve_offset(partition)
|
45
|
-
end
|
46
|
-
|
47
40
|
def test_unclaimed_partitions
|
48
|
-
partition40 = @
|
49
|
-
partition41 = @
|
50
|
-
partition42 = @
|
51
|
-
partition43 = @
|
41
|
+
partition40 = @topic4.partition(0)
|
42
|
+
partition41 = @topic4.partition(1)
|
43
|
+
partition42 = @topic4.partition(2)
|
44
|
+
partition43 = @topic4.partition(3)
|
52
45
|
|
53
46
|
refute @cg.active?
|
54
47
|
|
55
|
-
|
56
|
-
instance1.
|
57
|
-
|
58
|
-
instance2.
|
48
|
+
subscription = Kazoo::Subscription.build(@topic4)
|
49
|
+
instance1 = @cg.instantiate(subscription: subscription)
|
50
|
+
instance1.register
|
51
|
+
instance2 = @cg.instantiate(subscription: subscription)
|
52
|
+
instance2.register
|
59
53
|
|
60
54
|
assert @cg.active?
|
61
55
|
|
@@ -63,60 +57,118 @@ class FunctionalConsumergroupTest < Minitest::Test
|
|
63
57
|
instance2.claim_partition(partition41)
|
64
58
|
|
65
59
|
assert_equal 2, @cg.partition_claims.length
|
66
|
-
assert_equal [@
|
67
|
-
assert_equal @
|
60
|
+
assert_equal [@topic4], @cg.topics
|
61
|
+
assert_equal @topic4.partitions, @cg.partitions
|
68
62
|
|
69
63
|
assert_equal Set[partition42, partition43], Set.new(@cg.unclaimed_partitions)
|
70
64
|
end
|
71
65
|
|
72
|
-
def
|
73
|
-
|
74
|
-
|
66
|
+
def test_watch_instances
|
67
|
+
instance1 = @cg.instantiate(subscription: @topic1).register
|
68
|
+
instance2 = @cg.instantiate(subscription: @topic1).register
|
75
69
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
partition42 = Kazoo::Partition.new(topic4, 2)
|
80
|
-
partition43 = Kazoo::Partition.new(topic4, 3)
|
70
|
+
t = Thread.current
|
71
|
+
instances, cb = @cg.watch_instances { t.run if t.status == 'sleep' }
|
72
|
+
assert_equal Set[instance1, instance2], Set.new(instances)
|
81
73
|
|
82
|
-
|
74
|
+
Thread.new { instance2.deregister }
|
75
|
+
|
76
|
+
Thread.stop unless cb.completed?
|
77
|
+
|
78
|
+
assert assert_equal Set[instance1], Set.new(@cg.instances)
|
79
|
+
instance1.deregister
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_clean_topic_claims
|
83
|
+
deleted_topic = @cluster.topic('non_existing')
|
84
|
+
deleted_topic.destroy if deleted_topic.exists?
|
85
|
+
deleted_topic = @cluster.create_topic('non_existing', partitions: 1, replication_factor: 1)
|
86
|
+
|
87
|
+
subscription = Kazoo::Subscription.build([@topic4, deleted_topic])
|
88
|
+
@cg.instantiate(subscription: subscription).register
|
89
|
+
|
90
|
+
deleted_topic.destroy
|
91
|
+
|
92
|
+
assert_equal Set[@topic4, deleted_topic], Set.new(@cg.claimed_topics)
|
93
|
+
|
94
|
+
@cg.clean_topic_claims(Kazoo::Subscription.everything)
|
95
|
+
assert_equal Set[@topic4], Set.new(@cg.claimed_topics)
|
96
|
+
|
97
|
+
@cg.clean_topic_claims(Kazoo::Subscription.build([]))
|
98
|
+
assert_equal [], @cg.claimed_topics
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_retrieve_and_commit_offsets
|
102
|
+
partition = Kazoo::Partition.new(@topic1, 0)
|
103
|
+
|
104
|
+
assert_nil @cg.retrieve_offset(partition)
|
105
|
+
|
106
|
+
@cg.commit_offset(partition, 1234)
|
107
|
+
|
108
|
+
assert_equal 1234 + 1, @cg.retrieve_offset(partition)
|
109
|
+
|
110
|
+
@cg.reset_all_offsets
|
111
|
+
assert_nil @cg.retrieve_offset(partition)
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_retrieve_offsets_and_reset_all_offsets
|
115
|
+
partition10 = Kazoo::Partition.new(@topic1, 0)
|
116
|
+
partition40 = Kazoo::Partition.new(@topic4, 0)
|
117
|
+
partition41 = Kazoo::Partition.new(@topic4, 1)
|
118
|
+
partition42 = Kazoo::Partition.new(@topic4, 2)
|
119
|
+
partition43 = Kazoo::Partition.new(@topic4, 3)
|
120
|
+
|
121
|
+
assert @cg.retrieve_offsets(Kazoo::Subscription.everything).values.all? { |v| v.nil? }
|
83
122
|
|
84
123
|
@cg.commit_offset(partition10, 10)
|
85
124
|
@cg.commit_offset(partition40, 40)
|
86
125
|
@cg.commit_offset(partition41, 41)
|
87
126
|
@cg.commit_offset(partition42, 42)
|
88
|
-
@cg.commit_offset(partition43, 43)
|
89
127
|
|
90
|
-
offsets = @cg.
|
128
|
+
offsets = @cg.retrieve_offsets(Kazoo::Subscription.everything)
|
91
129
|
|
92
|
-
|
130
|
+
assert offsets.length >= 5
|
93
131
|
assert_equal 11, offsets[partition10]
|
94
132
|
assert_equal 41, offsets[partition40]
|
95
133
|
assert_equal 42, offsets[partition41]
|
96
134
|
assert_equal 43, offsets[partition42]
|
97
|
-
assert_equal
|
135
|
+
assert_equal nil, offsets[partition43]
|
136
|
+
|
137
|
+
offsets = @cg.retrieve_offsets(['test.1'])
|
138
|
+
|
139
|
+
assert_equal 1, offsets.length
|
140
|
+
assert_equal 11, offsets[partition10]
|
98
141
|
|
99
142
|
@cg.reset_all_offsets
|
100
|
-
|
143
|
+
assert @cg.retrieve_offsets(Kazoo::Subscription.everything).values.all? { |v| v.nil? }
|
101
144
|
end
|
102
145
|
|
103
|
-
def
|
104
|
-
|
146
|
+
def test_retrieve_all_offsets_and_cleanup_offsets
|
147
|
+
deleted_topic = @cluster.topic('non_existing')
|
148
|
+
deleted_topic.destroy if deleted_topic.exists?
|
149
|
+
deleted_topic = @cluster.create_topic('non_existing', partitions: 1, replication_factor: 1)
|
105
150
|
|
106
|
-
|
107
|
-
instance1.register([topic])
|
108
|
-
instance2 = @cg.instantiate
|
109
|
-
instance2.register([topic])
|
151
|
+
subscription = Kazoo::Subscription.build([@topic4, deleted_topic])
|
110
152
|
|
111
|
-
|
112
|
-
|
113
|
-
assert_equal Set[instance1, instance2], Set.new(instances)
|
153
|
+
@cg.commit_offset(@topic4.partition(0), 1234)
|
154
|
+
@cg.commit_offset(deleted_topic.partition(0), 1234)
|
114
155
|
|
115
|
-
|
156
|
+
deleted_topic.destroy
|
116
157
|
|
117
|
-
|
158
|
+
expected_offsets = {
|
159
|
+
deleted_topic.partition(0) => 1234 + 1,
|
160
|
+
@topic4.partition(0) => 1234 + 1,
|
161
|
+
}
|
162
|
+
assert_equal expected_offsets, @cg.retrieve_all_offsets
|
118
163
|
|
119
|
-
|
120
|
-
|
164
|
+
@cg.clean_stored_offsets(Kazoo::Subscription.everything)
|
165
|
+
|
166
|
+
expected_offsets = {
|
167
|
+
@topic4.partition(0) => 1234 + 1,
|
168
|
+
}
|
169
|
+
assert_equal expected_offsets, @cg.retrieve_all_offsets
|
170
|
+
|
171
|
+
@cg.clean_stored_offsets([])
|
172
|
+
assert_equal Hash.new, @cg.retrieve_all_offsets
|
121
173
|
end
|
122
174
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FunctionalTopicManagementTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
zookeeper = ENV["ZOOKEEPER_PEERS"] || "127.0.0.1:2181"
|
6
|
+
@cluster = Kazoo.connect(zookeeper)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_consumergroup_subscription
|
10
|
+
cg = @cluster.consumergroup('test.consumergroup.subscription')
|
11
|
+
cg.create
|
12
|
+
|
13
|
+
assert_raises(Kazoo::NoRunningInstances) { cg.subscription }
|
14
|
+
|
15
|
+
subscription_1 = Kazoo::Subscription.build('topic.1')
|
16
|
+
subscription_14 = Kazoo::Subscription.build(['topic.1', 'topic.4'])
|
17
|
+
|
18
|
+
instance1 = cg.instantiate(subscription: subscription_1).register
|
19
|
+
instance2 = cg.instantiate(subscription: subscription_1).register
|
20
|
+
instance3 = cg.instantiate(subscription: subscription_14).register
|
21
|
+
|
22
|
+
assert_raises(Kazoo::InconsistentSubscriptions) { cg.subscription }
|
23
|
+
|
24
|
+
instance3.deregister
|
25
|
+
|
26
|
+
assert_equal subscription_1, cg.subscription
|
27
|
+
assert_equal subscription_1, instance1.subscription
|
28
|
+
|
29
|
+
instance2.deregister
|
30
|
+
|
31
|
+
assert_equal subscription_1, cg.subscription
|
32
|
+
assert_equal subscription_1, instance1.subscription
|
33
|
+
|
34
|
+
instance1.deregister
|
35
|
+
|
36
|
+
assert_raises(Kazoo::NoRunningInstances) { cg.subscription }
|
37
|
+
ensure
|
38
|
+
cg.destroy if cg.exists?
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_subscription_topics_and_partitions
|
42
|
+
topic1 = @cluster.create_topic('test.kazoo.subscription.1', partitions: 1, replication_factor: 1)
|
43
|
+
topic2 = @cluster.create_topic('test.kazoo.subscription.2', partitions: 2, replication_factor: 1)
|
44
|
+
topic3 = @cluster.create_topic('test.kazoo.non_matching', partitions: 1, replication_factor: 1)
|
45
|
+
|
46
|
+
subscription = Kazoo::Subscription.build(/^test\.kazoo\.subscription\.\d+/)
|
47
|
+
assert_equal Set[topic1, topic2], Set.new(subscription.topics(@cluster))
|
48
|
+
assert_equal Set[topic1.partition(0), topic2.partition(0), topic2.partition(1)], Set.new(subscription.partitions(@cluster))
|
49
|
+
|
50
|
+
topic2.destroy
|
51
|
+
|
52
|
+
assert_equal Set[topic1], Set.new(subscription.topics(@cluster))
|
53
|
+
assert_equal Set[topic1.partition(0)], Set.new(subscription.partitions(@cluster))
|
54
|
+
|
55
|
+
topic1.destroy
|
56
|
+
|
57
|
+
assert_equal Set[], Set.new(subscription.topics(@cluster))
|
58
|
+
assert_equal Set[], Set.new(subscription.partitions(@cluster))
|
59
|
+
|
60
|
+
topic3.destroy
|
61
|
+
end
|
62
|
+
end
|
@@ -4,6 +4,12 @@ class FunctionalTopicManagementTest < Minitest::Test
|
|
4
4
|
def setup
|
5
5
|
zookeeper = ENV["ZOOKEEPER_PEERS"] || "127.0.0.1:2181"
|
6
6
|
@cluster = Kazoo.connect(zookeeper)
|
7
|
+
|
8
|
+
topic = @cluster.topic('test.kazoo')
|
9
|
+
topic.destroy if topic.exists?
|
10
|
+
|
11
|
+
topic = @cluster.topic('test.kazoo.config')
|
12
|
+
topic.destroy if topic.exists?
|
7
13
|
end
|
8
14
|
|
9
15
|
def test_create_and_delete_topic
|
@@ -14,16 +20,33 @@ class FunctionalTopicManagementTest < Minitest::Test
|
|
14
20
|
assert_equal 8, topic.partitions.length
|
15
21
|
|
16
22
|
topic.destroy
|
23
|
+
|
24
|
+
refute topic.exists?
|
25
|
+
refute @cluster.topics.key?(topic.name)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_adding_partitions_to_topic
|
29
|
+
topic = @cluster.create_topic('test.kazoo', partitions: 2, replication_factor: 1)
|
30
|
+
|
31
|
+
topic.add_partitions(partitions: 2, replication_factor: 1)
|
32
|
+
|
33
|
+
assert topic.partitions.all? { |partition| @cluster.brokers.values.include?(partition.leader) }
|
34
|
+
assert_equal 4, topic.partitions.length
|
35
|
+
|
17
36
|
@cluster.reset_metadata
|
18
37
|
|
38
|
+
assert topic.partitions.all? { |partition| @cluster.brokers.values.include?(partition.leader) }
|
39
|
+
assert_equal 4, topic.partitions.length
|
40
|
+
|
41
|
+
topic.destroy
|
42
|
+
|
19
43
|
refute topic.exists?
|
20
44
|
refute @cluster.topics.key?(topic.name)
|
21
45
|
end
|
22
46
|
|
23
47
|
def test_topic_config_management
|
24
|
-
topic = @cluster.create_topic('test.kazoo.config', partitions: 1, replication_factor: 1)
|
48
|
+
topic = @cluster.create_topic('test.kazoo.config', partitions: 1, replication_factor: 1, config: { "flush.messages" => 1, "max.message.bytes" => 64000 })
|
25
49
|
|
26
|
-
topic.write_config("flush.messages" => 1, "max.message.bytes" => 64000)
|
27
50
|
assert_equal "1", topic.config["flush.messages"]
|
28
51
|
assert_equal "64000", topic.config["max.message.bytes"]
|
29
52
|
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ReplicaAssignerTest < Minitest::Test
|
4
|
+
include MockCluster
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@cluster = mock_cluster
|
8
|
+
@replica_assigner = Kazoo::ReplicaAssigner.new(@cluster)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_initial_counts
|
12
|
+
cluster_leader_count = @cluster.partitions.length
|
13
|
+
assert_equal cluster_leader_count, @replica_assigner.cluster_leader_count
|
14
|
+
|
15
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[1]]
|
16
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[2]]
|
17
|
+
assert_equal 1, @replica_assigner.broker_leaders[@cluster.brokers[3]]
|
18
|
+
|
19
|
+
cluster_replica_count = @cluster.partitions.inject(0) { |sum, p| sum + p.replicas.length }
|
20
|
+
assert_equal cluster_replica_count, @replica_assigner.cluster_replica_count
|
21
|
+
|
22
|
+
assert_equal 3, @replica_assigner.broker_replicas[@cluster.brokers[1]]
|
23
|
+
assert_equal 4, @replica_assigner.broker_replicas[@cluster.brokers[2]]
|
24
|
+
assert_equal 3, @replica_assigner.broker_replicas[@cluster.brokers[3]]
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_assign_replicas_validation
|
28
|
+
assert_raises(Kazoo::ValidationError) { @replica_assigner.assign(4) }
|
29
|
+
assert_raises(Kazoo::ValidationError) { @replica_assigner.assign(0) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_assign_replicas_deterministically
|
33
|
+
replicas_1 = @replica_assigner.assign(2)
|
34
|
+
assert_equal [@cluster.brokers[3], @cluster.brokers[1]], replicas_1
|
35
|
+
|
36
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[1]]
|
37
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[2]]
|
38
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[3]]
|
39
|
+
|
40
|
+
assert_equal 4, @replica_assigner.broker_replicas[@cluster.brokers[1]]
|
41
|
+
assert_equal 4, @replica_assigner.broker_replicas[@cluster.brokers[2]]
|
42
|
+
assert_equal 4, @replica_assigner.broker_replicas[@cluster.brokers[3]]
|
43
|
+
|
44
|
+
replicas_2 = @replica_assigner.assign(3)
|
45
|
+
assert_equal [@cluster.brokers[1], @cluster.brokers[2], @cluster.brokers[3]], replicas_2
|
46
|
+
|
47
|
+
assert_equal 3, @replica_assigner.broker_leaders[@cluster.brokers[1]]
|
48
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[2]]
|
49
|
+
assert_equal 2, @replica_assigner.broker_leaders[@cluster.brokers[3]]
|
50
|
+
|
51
|
+
assert_equal 5, @replica_assigner.broker_replicas[@cluster.brokers[1]]
|
52
|
+
assert_equal 5, @replica_assigner.broker_replicas[@cluster.brokers[2]]
|
53
|
+
assert_equal 5, @replica_assigner.broker_replicas[@cluster.brokers[3]]
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_assign_replicas_evenly
|
58
|
+
initial_leaders = @replica_assigner.cluster_leader_count
|
59
|
+
initial_replicas = @replica_assigner.cluster_replica_count
|
60
|
+
|
61
|
+
1000.times do
|
62
|
+
replicas = @replica_assigner.assign(2)
|
63
|
+
assert_equal 2, replicas.length
|
64
|
+
end
|
65
|
+
|
66
|
+
# We expect the cluster-wide counters to be increased by the expected amount of replicas
|
67
|
+
final_leaders = @replica_assigner.cluster_leader_count
|
68
|
+
final_replicas = @replica_assigner.cluster_replica_count
|
69
|
+
|
70
|
+
assert_equal final_leaders, initial_leaders + 1000
|
71
|
+
assert_equal final_replicas, initial_replicas + 2000
|
72
|
+
|
73
|
+
# For the entire cluster, the number of leaders and replicas should be even for every broker.
|
74
|
+
leader_diff = @replica_assigner.broker_leaders.values.max - @replica_assigner.broker_leaders.values.min
|
75
|
+
assert leader_diff >= 0 && leader_diff <= 1
|
76
|
+
|
77
|
+
replica_diff = @replica_assigner.broker_replicas.values.max - @replica_assigner.broker_replicas.values.min
|
78
|
+
assert replica_diff >= 0 && replica_diff <= 1
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SubscriptionTest < Minitest::Test
|
4
|
+
include MockCluster
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@cluster = mock_cluster
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_static_subscription_topics
|
11
|
+
subscription = Kazoo::Subscription.build(['test.1', 'nonexisting'])
|
12
|
+
topics = subscription.topics(@cluster)
|
13
|
+
assert_equal Set['test.1'], Set.new(topics.map(&:name))
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_pattern_subscription_topics
|
17
|
+
subscription = Kazoo::Subscription.build(/^test\.\d+/, pattern: :white_list)
|
18
|
+
topics = subscription.topics(@cluster)
|
19
|
+
assert_equal Set['test.1', 'test.4'], Set.new(topics.map(&:name))
|
20
|
+
|
21
|
+
subscription = Kazoo::Subscription.build(/\.4/, pattern: :black_list)
|
22
|
+
topics = subscription.topics(@cluster)
|
23
|
+
assert_equal Set['test.1'], Set.new(topics.map(&:name))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_equality
|
27
|
+
subscription1 = Kazoo::Subscription.build(/^test\.\d+/, pattern: :white_list)
|
28
|
+
subscription2 = Kazoo::Subscription.build(/^test\.\d+/, pattern: :white_list)
|
29
|
+
subscription3 = Kazoo::Subscription.build(/^test\.\d+/, pattern: :black_list)
|
30
|
+
subscription4 = Kazoo::Subscription.build(/^test\.\d*/, pattern: :white_list)
|
31
|
+
assert subscription1 == subscription2
|
32
|
+
refute subscription1 == subscription3
|
33
|
+
refute subscription1 == subscription4
|
34
|
+
|
35
|
+
subscription1 = Kazoo::Subscription.build(:'test.1')
|
36
|
+
subscription2 = Kazoo::Subscription.build(['test.1'])
|
37
|
+
subscription3 = Kazoo::Subscription.build(['test.1', 'test.4'])
|
38
|
+
assert subscription1 == subscription2
|
39
|
+
refute subscription1 == subscription3
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_subscription_from_json
|
43
|
+
timestamp_msec = 628232400123
|
44
|
+
timestamp = Time.at(BigDecimal.new(timestamp_msec) / BigDecimal.new(1000))
|
45
|
+
|
46
|
+
json_payload = JSON.generate(
|
47
|
+
version: 1,
|
48
|
+
timestamp: timestamp_msec,
|
49
|
+
pattern: "static",
|
50
|
+
subscription: { 'topic.1' => 1, 'topic.4' => 1 },
|
51
|
+
)
|
52
|
+
|
53
|
+
subscription = Kazoo::Subscription.from_json(json_payload)
|
54
|
+
assert_kind_of Kazoo::StaticSubscription, subscription
|
55
|
+
assert_equal timestamp, subscription.timestamp
|
56
|
+
assert_equal 1, subscription.version
|
57
|
+
assert_equal Set['topic.1', 'topic.4'], Set.new(subscription.topic_names)
|
58
|
+
|
59
|
+
json_payload = JSON.generate(
|
60
|
+
version: 1,
|
61
|
+
timestamp: timestamp_msec,
|
62
|
+
pattern: "black_list",
|
63
|
+
subscription: { "^test\\.\\d+" => 1 },
|
64
|
+
)
|
65
|
+
|
66
|
+
subscription = Kazoo::Subscription.from_json(json_payload)
|
67
|
+
assert_kind_of Kazoo::PatternSubscription, subscription
|
68
|
+
assert_equal timestamp, subscription.timestamp
|
69
|
+
assert_equal 1, subscription.version
|
70
|
+
assert subscription.black_list?
|
71
|
+
assert_equal %r{^test\.\d+}, subscription.regexp
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_single_topic_static_subscription_json
|
75
|
+
subscription = Kazoo::Subscription.build('topic')
|
76
|
+
json = subscription.to_json
|
77
|
+
|
78
|
+
parsed_subscription = JSON.parse(json)
|
79
|
+
assert_equal 1, parsed_subscription.fetch('version')
|
80
|
+
assert_equal 'static', parsed_subscription.fetch('pattern')
|
81
|
+
assert_kind_of Integer, parsed_subscription.fetch('timestamp')
|
82
|
+
|
83
|
+
assert_kind_of Hash, parsed_subscription.fetch('subscription')
|
84
|
+
assert_equal 1, parsed_subscription.fetch('subscription').length
|
85
|
+
assert_equal 1, parsed_subscription.fetch('subscription').fetch('topic')
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_multi_topic_static_subscription_json
|
89
|
+
subscription = Kazoo::Subscription.build([:topic1, :topic2])
|
90
|
+
json = subscription.to_json
|
91
|
+
|
92
|
+
parsed_subscription = JSON.parse(json)
|
93
|
+
assert_equal 1, parsed_subscription.fetch('version')
|
94
|
+
assert_equal 'static', parsed_subscription.fetch('pattern')
|
95
|
+
assert_kind_of Integer, parsed_subscription.fetch('timestamp')
|
96
|
+
|
97
|
+
assert_kind_of Hash, parsed_subscription.fetch('subscription')
|
98
|
+
assert_equal 2, parsed_subscription.fetch('subscription').length
|
99
|
+
assert_equal 1, parsed_subscription.fetch('subscription').fetch('topic1')
|
100
|
+
assert_equal 1, parsed_subscription.fetch('subscription').fetch('topic2')
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_whitelist_subscription_json
|
104
|
+
subscription = Kazoo::Subscription.build(/^topic/)
|
105
|
+
json = subscription.to_json
|
106
|
+
|
107
|
+
parsed_subscription = JSON.parse(json)
|
108
|
+
assert_equal 1, parsed_subscription.fetch('version')
|
109
|
+
assert_equal 'white_list', parsed_subscription.fetch('pattern')
|
110
|
+
assert_kind_of Integer, parsed_subscription.fetch('timestamp')
|
111
|
+
|
112
|
+
assert_kind_of Hash, parsed_subscription.fetch('subscription')
|
113
|
+
assert_equal 1, parsed_subscription.fetch('subscription').length
|
114
|
+
|
115
|
+
assert_equal ["^topic"], parsed_subscription.fetch('subscription').keys
|
116
|
+
assert_equal [1], parsed_subscription.fetch('subscription').values
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_blacklist_subscription_json
|
120
|
+
subscription = Kazoo::Subscription.build(/^topic/, pattern: :black_list)
|
121
|
+
json = subscription.to_json
|
122
|
+
|
123
|
+
parsed_subscription = JSON.parse(json)
|
124
|
+
assert_equal 1, parsed_subscription.fetch('version')
|
125
|
+
assert_equal 'black_list', parsed_subscription.fetch('pattern')
|
126
|
+
assert_kind_of Integer, parsed_subscription.fetch('timestamp')
|
127
|
+
|
128
|
+
assert_kind_of Hash, parsed_subscription.fetch('subscription')
|
129
|
+
assert_equal 1, parsed_subscription.fetch('subscription').length
|
130
|
+
|
131
|
+
assert_equal ["^topic"], parsed_subscription.fetch('subscription').keys
|
132
|
+
assert_equal [1], parsed_subscription.fetch('subscription').values
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_subscription_has_topic
|
136
|
+
static_subscription = Kazoo::Subscription.build(%w[topic.1 topic.4], pattern: :black_list)
|
137
|
+
assert static_subscription.has_topic?(@cluster.topic('topic.1'))
|
138
|
+
refute static_subscription.has_topic?(@cluster.topic('test.topic'))
|
139
|
+
|
140
|
+
whitelist_subscription = Kazoo::Subscription.build(/^topic\./, pattern: :white_list)
|
141
|
+
assert whitelist_subscription.has_topic?(@cluster.topic('topic.1'))
|
142
|
+
refute whitelist_subscription.has_topic?(@cluster.topic('test.topic'))
|
143
|
+
|
144
|
+
blacklist_subscription = Kazoo::Subscription.build(/^topic\./, pattern: :black_list)
|
145
|
+
refute blacklist_subscription.has_topic?(@cluster.topic('topic.1'))
|
146
|
+
assert blacklist_subscription.has_topic?(@cluster.topic('test.topic'))
|
147
|
+
end
|
148
|
+
end
|