Skip to content

Commit

Permalink
refactor(watch): rename Etcd::Watch::Watcher::WatchFilter -> `Etcd:…
Browse files Browse the repository at this point in the history
…:Watch::Filter`, and spec usage of filters
  • Loading branch information
caspiano committed Feb 6, 2020
1 parent 6f0e2de commit d9288cf
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: etcd
version: 0.2.4
version: 0.3.0

authors:
- Caspian Baska <[email protected]>
Expand Down
66 changes: 66 additions & 0 deletions spec/watch_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,71 @@ module Etcd

watcher.stop
end

describe Etcd::Watch::Filter do
it "ignores put events with NOPUT" do
ttl = 5_i64

key0, value0 = "#{TEST_PREFIX}/foo", "bar"
key1, value1 = "#{TEST_PREFIX}/foot", "bath"

received = [] of Etcd::Model::WatchEvent
watcher = Etcd.from_env.watch.watch_prefix(key0, filters: [Etcd::Watch::Filter::NOPUT]) do |events|
received += events
end

begin
spawn { watcher.start }
Fiber.yield
rescue
end

client = Etcd.from_env

lease = client.lease.grant ttl
client.kv.put(key0, value0, lease: lease[:id])
client.kv.delete(key0)
client.kv.put(key1, value1, lease: lease[:id])

received.size.should eq 1

watcher.stop
end

it "ignores delete events with NODELETE" do
ttl = 5_i64

key0, value0 = "#{TEST_PREFIX}/foo", "bar"
key1, value1 = "#{TEST_PREFIX}/foot", "bath"

received = [] of Etcd::Model::WatchEvent
watcher = Etcd.from_env.watch.watch_prefix(key0, filters: [Etcd::Watch::Filter::NODELETE]) do |events|
received += events
end

begin
spawn { watcher.start }
Fiber.yield
rescue
end

client = Etcd.from_env
lease = client.lease.grant ttl
client.kv.put(key0, value0, lease: lease[:id])
client.kv.put(key1, value1, lease: lease[:id])
client.kv.delete(key0)

received.size.should eq 2
first, second = received

first.kv.key.should eq key0
first.kv.value.should eq value0

second.kv.key.should eq key1
second.kv.value.should eq value1

watcher.stop
end
end
end
end
20 changes: 10 additions & 10 deletions src/etcd/watch.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ require "./model/watch"
class Etcd::Watch
include Utils

# Types for watch event filters
enum Filter
NOPUT # filter put events
NODELETE # filter delete events
end

private getter client : Etcd::Client

def initialize(@client = Etcd::Client.new)
Expand All @@ -15,7 +21,7 @@ class Etcd::Watch
# Exposes a synchronous interface to the watch session via `Etcd::Watcher`
#
# opts
# filters filters filter the events at server side before it sends back to the watcher. [WatchFilter]
# filters filters filter the events at server side before it sends back to the watcher. [Watch::Filter]
# start_revision start_revision is an optional revision to watch from (inclusive). No start_revision is "now". Int64
# progress_notify progress_notify is set so that the etcd server will periodically send a WatchResponse with no events to the new watcher
# if there are no recent events. It is useful when clients wish to recover a disconnected watcher starting from
Expand All @@ -32,7 +38,7 @@ class Etcd::Watch
#
# opts
# range_end range_end is the end of the range [key, range_end) to watch.
# filters filter the events at server side before it sends back to the watcher. [WatchFilter]
# filters filter the events at server side before it sends back to the watcher. [Watch::Filter]
# start_revision start_revision is an optional revision to watch from (inclusive). No start_revision is "now". Int64
# progress_notify progress_notify is set so that the etcd server will periodically send a WatchResponse with no events to the new watcher
# if there are no recent events. It is useful when clients wish to recover a disconnected watcher starting from
Expand All @@ -41,7 +47,7 @@ class Etcd::Watch
def watch(
key,
range_end : String? = nil,
filters : Array(Watcher::WatchFilter)? = nil,
filters : Array(Watch::Filter)? = nil,
start_revision : Int64? = nil,
progress_notify : Bool? = nil,
base64_keys : Bool = true,
Expand Down Expand Up @@ -79,16 +85,10 @@ class Etcd::Watch
private getter api : Etcd::Api
private getter block : Proc(Array(Model::WatchEvent), Void)
private getter range_end : String?
private getter filters : Array(WatchFilter)?
private getter filters : Array(Watch::Filter)?
private getter start_revision : Int64?
private getter progress_notify : Bool?

# Types for watch event filters
enum WatchFilter
NOPUT # filter put events
NODELETE # filter delete events
end

getter watching : Bool = false

def initialize(
Expand Down

0 comments on commit d9288cf

Please sign in to comment.