Skip to content

Commit

Permalink
watcher resyncs on cleared index, ref #31
Browse files Browse the repository at this point in the history
  • Loading branch information
stianeikeland committed Oct 2, 2014
1 parent d734827 commit 507f32e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/watcher.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,18 @@ class Watcher extends EventEmitter
@emit 'error', error
@_retry()

_resync: (err) =>
@index = err.error.index
@retryAttempts = 0
@emit 'resync', err
@_watch()

_respHandler: (err, val, headers) =>
return if @stopped

if err
if err?.errorCode is 401 and err.error?.index?
@_resync err
else if err
@_error err
else if headers?['x-etcd-index']? and not val?
@_missingValue headers
Expand Down
47 changes: 46 additions & 1 deletion test/watcher.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'should'
should = require 'should'
nock = require 'nock'

Etcd = require '../src/index'
Watcher = require '../src/watcher.coffee'

class FakeEtcd
Expand Down Expand Up @@ -121,3 +124,45 @@ describe 'Watcher', ->
w.stop()


describe 'Watcher resync', ->

getNock = ->
nock 'http://127.0.0.1:4001'

it 'should resync if index is outdated and cleared', (done) ->
# getNock()
# .get('/v2/keys/key')
# .reply(200, {})

getNock()
.get('/v2/keys/key?waitIndex=0&wait=true')
.reply(401, {
errorCode: 401,
message: "The event in requested index is outdated and cleared",
cause: "the requested history has been cleared [1007/4]",
index: 2006
})
.get('/v2/keys/key?waitIndex=2006&wait=true')
.reply(200, {
action:"set",
node: {
key: "/key",
value: "banan",
modifiedIndex: 2013,
createdIndex: 2013
},
prevNode: {
key: "/key",
value: "2",
modifiedIndex: 5,
createdIndex: 5
}
})

etcd = new Etcd
w = etcd.watcher 'key', 0
w.on 'change', (res) ->
res.node.value.should.equal 'banan'
done()

it 'should discover if it missed any updates on resync'

0 comments on commit 507f32e

Please sign in to comment.