Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changes are not delivered after a sync message wasn't delivered #368

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions packages/automerge-repo/src/helpers/DummyNetworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ export class DummyNetworkAdapter extends NetworkAdapter {
#sendMessage?: SendMessageFn

#ready = false
#isDroppingMessages = false
#readyResolver?: () => void
#readyPromise: Promise<void> = new Promise<void>(resolve => {
this.#readyResolver = resolve
})

#droppedMessages: Message[] = []

dropMessages(drop: boolean) {
this.#isDroppingMessages = drop
}

getDroppedMessages() {
return [...this.#droppedMessages]
}

isReady() {
return this.#ready
}
Expand Down Expand Up @@ -49,11 +60,19 @@ export class DummyNetworkAdapter extends NetworkAdapter {
}

override send(message: Message) {
this.#sendMessage?.(message)
if (this.#isDroppingMessages) {
this.#droppedMessages.push(message)
} else {
this.#sendMessage?.(message)
}
}

receive(message: Message) {
this.emit("message", message)
if (this.#isDroppingMessages) {
this.#droppedMessages.push(message)
} else {
this.emit("message", message)
}
}

static createConnectedPair({ latency = 10 }: { latency?: number } = {}) {
Expand Down
5 changes: 5 additions & 0 deletions packages/automerge-repo/src/synchronizer/DocSynchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ export class DocSynchronizer extends Synchronizer {

this.#withSyncState(message.senderId, syncState => {
this.#handle.update(doc => {
// Retry if message sending failed.
for (const need of A.decodeSyncMessage(message.data).need) {
delete (syncState.sentHashes as any)[need]
}

const [newDoc, newSyncState] = A.receiveSyncMessage(
doc,
syncState,
Expand Down
76 changes: 76 additions & 0 deletions packages/automerge-repo/test/Repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,82 @@ describe("Repo", () => {
assert.equal(bobDoc.isReady(), true)
})

it("changes get replicated after the next change if a sync message was dropped", async () => {
const alice = "alice" as PeerId
const bob = "bob" as PeerId
const [aliceAdapter, bobAdapter] = DummyNetworkAdapter.createConnectedPair()
const aliceRepo = new Repo({
network: [aliceAdapter],
peerId: alice,
})
const bobRepo = new Repo({
network: [bobAdapter],
peerId: bob,
})
aliceAdapter.peerCandidate(bob)
bobAdapter.peerCandidate(alice)

const bobHandle = bobRepo.create<TestDoc>()
bobHandle.change(d => {
d.foo = "bar"
})
const aliceHandle = await aliceRepo.find(bobHandle.documentId)
await eventPromise(aliceHandle, "heads-changed")
assert.deepEqual(aliceHandle.docSync(), bobHandle.docSync())

bobAdapter.dropMessages(true)
bobHandle.change(d => {
d.bar = "foo"
})
await pause(10)
assert.equal(bobAdapter.getDroppedMessages()[0].type, "sync")

bobAdapter.dropMessages(false)
bobHandle.change(d => {
d.baz = "42"
})
await eventPromise(aliceHandle, "heads-changed")
})

it("changes get replicated the peer's next change if a sync message was dropped", async () => {
const alice = "alice" as PeerId
const bob = "bob" as PeerId
const [aliceAdapter, bobAdapter] = DummyNetworkAdapter.createConnectedPair()
const aliceRepo = new Repo({
network: [aliceAdapter],
peerId: alice,
})
const bobRepo = new Repo({
network: [bobAdapter],
peerId: bob,
})
aliceAdapter.peerCandidate(bob)
bobAdapter.peerCandidate(alice)

const bobHandle = bobRepo.create<TestDoc>()
bobHandle.change(d => {
d.foo = "bar"
})
const aliceHandle = await aliceRepo.find(bobHandle.documentId)
await eventPromise(aliceHandle, "heads-changed")
assert.deepEqual(aliceHandle.docSync(), bobHandle.docSync())

bobAdapter.dropMessages(true)
bobHandle.change(d => {
d.bar = "foo"
})
await pause(10)
assert.equal(bobAdapter.getDroppedMessages()[0].type, "sync")

bobAdapter.dropMessages(false)
aliceHandle.change(d => {
d.baz = "42"
})
await eventPromise(bobHandle, "heads-changed")
await pause(200)
assert.deepEqual(aliceHandle.docSync(), bobHandle.docSync())
})

describe("with peers (mesh network)", () => {
const setup = async () => {
// Set up three repos; connect Alice to Bob, Bob to Charlie, and Alice to Charlie
Expand Down
Loading