File: sync_spec.rb

package info (click to toggle)
ruby-flipper 0.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,288 kB
  • sloc: ruby: 16,377; sh: 61; javascript: 24; makefile: 14
file content (200 lines) | stat: -rw-r--r-- 6,510 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'flipper/adapters/sync'
require 'flipper/adapters/operation_logger'
require 'active_support/notifications'

RSpec.describe Flipper::Adapters::Sync do
  let(:local_adapter) do
    Flipper::Adapters::OperationLogger.new Flipper::Adapters::Memory.new
  end
  let(:remote_adapter) do
    Flipper::Adapters::OperationLogger.new Flipper::Adapters::Memory.new
  end
  let(:local) { Flipper.new(local_adapter) }
  let(:remote) { Flipper.new(remote_adapter) }
  let(:sync) { Flipper.new(subject) }

  subject do
    described_class.new(local_adapter, remote_adapter, interval: 1)
  end

  it_should_behave_like 'a flipper adapter'

  context 'when local has never been synced' do
    it 'syncs boolean' do
      remote.enable(:search)
      expect(sync[:search].boolean_value).to be(true)
      expect(subject.features.sort).to eq(%w(search))
    end

    it 'syncs actor' do
      actor = Flipper::Actor.new("User;1000")
      remote.enable_actor(:search, actor)
      expect(sync[:search].actors_value).to eq(Set[actor.flipper_id])
      expect(subject.features.sort).to eq(%w(search))
    end

    it 'syncs group' do
      remote.enable_group(:search, :staff)
      expect(sync[:search].groups_value).to eq(Set["staff"])
      expect(subject.features.sort).to eq(%w(search))
    end

    it 'syncs percentage of actors' do
      remote.enable_percentage_of_actors(:search, 25)
      expect(sync[:search].percentage_of_actors_value).to eq(25)
      expect(subject.features.sort).to eq(%w(search))
    end

    it 'syncs percentage of time' do
      remote.enable_percentage_of_time(:search, 15)
      expect(sync[:search].percentage_of_time_value).to eq(15)
      expect(subject.features.sort).to eq(%w(search))
    end
  end

  it 'enables boolean locally when remote feature boolean enabled' do
    remote.disable(:search)
    local.disable(:search)
    remote.enable(:search)
    subject # initialize forces sync
    expect(local[:search].boolean_value).to be(true)
  end

  it 'disables boolean locally when remote feature disabled' do
    remote.enable(:search)
    local.enable(:search)
    remote.disable(:search)
    subject # initialize forces sync
    expect(local[:search].boolean_value).to be(false)
  end

  it 'adds local actor when remote actor is added' do
    actor = Flipper::Actor.new("User;235")
    remote.enable_actor(:search, actor)
    subject # initialize forces sync
    expect(local[:search].actors_value).to eq(Set[actor.flipper_id])
  end

  it 'removes local actor when remote actor is removed' do
    actor = Flipper::Actor.new("User;235")
    remote.enable_actor(:search, actor)
    local.enable_actor(:search, actor)
    remote.disable(:search, actor)
    subject # initialize forces sync
    expect(local[:search].actors_value).to eq(Set.new)
  end

  it 'adds local group when remote group is added' do
    group = Flipper::Types::Group.new(:staff)
    remote.enable_group(:search, group)
    subject # initialize forces sync
    expect(local[:search].groups_value).to eq(Set["staff"])
  end

  it 'removes local group when remote group is removed' do
    group = Flipper::Types::Group.new(:staff)
    remote.enable_group(:search, group)
    local.enable_group(:search, group)
    remote.disable(:search, group)
    subject # initialize forces sync
    expect(local[:search].groups_value).to eq(Set.new)
  end

  it 'updates percentage of actors when remote is updated' do
    remote.enable_percentage_of_actors(:search, 10)
    local.enable_percentage_of_actors(:search, 10)
    remote.enable_percentage_of_actors(:search, 15)
    subject # initialize forces sync
    expect(local[:search].percentage_of_actors_value).to eq(15)
  end

  it 'updates percentage of time when remote is updated' do
    remote.enable_percentage_of_time(:search, 10)
    local.enable_percentage_of_time(:search, 10)
    remote.enable_percentage_of_time(:search, 15)
    subject # initialize forces sync
    expect(local[:search].percentage_of_time_value).to eq(15)
  end

  context 'when local and remote match' do
    it 'does not update boolean enabled' do
      local.enable(:search)
      remote.enable(:search)
      local_adapter.reset
      subject # initialize forces sync
      expect(local_adapter.count(:enable)).to be(0)
    end

    it 'does not update boolean disabled' do
      local.disable(:search)
      remote.disable(:search)
      local_adapter.reset
      subject # initialize forces sync
      expect(local_adapter.count(:disable)).to be(0)
    end

    it 'does not update actors' do
      actor = Flipper::Actor.new("User;235")
      local.enable_actor(:search, actor)
      remote.enable_actor(:search, actor)
      local_adapter.reset
      subject # initialize forces sync
      expect(local_adapter.count(:enable)).to be(0)
      expect(local_adapter.count(:disable)).to be(0)
    end

    it 'does not update groups' do
      group = Flipper::Types::Group.new(:staff)
      local.enable_group(:search, group)
      remote.enable_group(:search, group)
      local_adapter.reset
      subject # initialize forces sync
      expect(local_adapter.count(:enable)).to be(0)
      expect(local_adapter.count(:disable)).to be(0)
    end

    it 'does not update percentage of actors' do
      local.enable_percentage_of_actors(:search, 10)
      remote.enable_percentage_of_actors(:search, 10)
      local_adapter.reset
      subject # initialize forces sync
      expect(local_adapter.count(:enable)).to be(0)
      expect(local_adapter.count(:disable)).to be(0)
    end

    it 'does not update percentage of time' do
      local.enable_percentage_of_time(:search, 10)
      remote.enable_percentage_of_time(:search, 10)
      local_adapter.reset
      subject # initialize forces sync
      expect(local_adapter.count(:enable)).to be(0)
      expect(local_adapter.count(:disable)).to be(0)
    end
  end

  it 'synchronizes for #features' do
    expect(subject).to receive(:synchronize)
    subject.features
  end

  it 'synchronizes for #get' do
    expect(subject).to receive(:synchronize)
    subject.get sync[:search]
  end

  it 'synchronizes for #get_multi' do
    expect(subject).to receive(:synchronize)
    subject.get_multi [sync[:search]]
  end

  it 'synchronizes for #get_all' do
    expect(subject).to receive(:synchronize)
    subject.get_all
  end

  it 'does not raise sync exceptions' do
    exception = StandardError.new
    expect(remote_adapter).to receive(:get_all).and_raise(exception)
    expect { subject.get_all }.not_to raise_error
  end
end