-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
271 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export class LWWRegister<T> { | ||
private _element: T; | ||
private _timestamp: number; | ||
private _nodeId: string; | ||
|
||
constructor(element: T, nodeId: string) { | ||
this._element = element; | ||
this._timestamp = Date.now(); | ||
this._nodeId = nodeId; | ||
} | ||
|
||
assign(element: T, nodeId: string): void { | ||
this._element = element; | ||
this._timestamp = Date.now(); | ||
this._nodeId = nodeId; | ||
} | ||
|
||
getElement(): T { | ||
return this._element; | ||
} | ||
|
||
getTimestamp(): number { | ||
return this._timestamp; | ||
} | ||
|
||
getNodeId(): string { | ||
return this._nodeId; | ||
} | ||
|
||
compare(register: LWWRegister<T>): boolean { | ||
return (this._timestamp <= register.getTimestamp()); | ||
} | ||
|
||
merge(register: LWWRegister<T>): void { | ||
const otherTimestamp = register.getTimestamp(); | ||
const otherNodeId = register.getNodeId(); | ||
if (otherTimestamp < this._timestamp) { | ||
return; | ||
} | ||
if (otherTimestamp === this._timestamp && otherNodeId <= this._nodeId) { | ||
return; | ||
} | ||
this._element = register.getElement(); | ||
this._timestamp = otherTimestamp; | ||
this._nodeId = otherNodeId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { describe, test, expect, beforeEach, vi } from "vitest"; | ||
import { LWWRegister } from "../src/builtins/LWWRegister/index.ts"; | ||
|
||
describe('LWW-Register Tests', () => { | ||
test('Test Assign', () => { | ||
let register1 = new LWWRegister<string>("alice", "node1"); | ||
|
||
expect(register1.getElement()).toBe("alice"); | ||
expect(register1.getNodeId()).toEqual("node1"); | ||
register1.assign("bob", "node2"); | ||
expect(register1.getElement()).toBe("bob"); | ||
expect(register1.getNodeId()).toEqual("node2"); | ||
}); | ||
|
||
test('Test Compare', () => { | ||
vi.useFakeTimers(); | ||
const date = new Date(2000,1,1,13); | ||
vi.setSystemTime(date); | ||
|
||
let register1 = new LWWRegister<string>("alice", "node1"); | ||
|
||
vi.useRealTimers(); | ||
|
||
let register2 = new LWWRegister<string>("alice", "node2"); | ||
|
||
expect(register1.compare(register2)).toEqual(true); | ||
expect(register2.compare(register1)).toEqual(false); | ||
}); | ||
|
||
test('Test Merge', () => { | ||
let register1 = new LWWRegister<string>("alice", "node1"); | ||
let register2 = new LWWRegister<string>("bob", "node2"); | ||
|
||
register1.merge(register2); | ||
expect(register1.getElement()).toEqual("bob"); | ||
expect(register2.getNodeId()).toEqual("node2"); | ||
|
||
register2.merge(register1); | ||
expect(register1.getElement()).toEqual("bob"); | ||
expect(register2.getNodeId()).toEqual("node2"); | ||
}); | ||
|
||
test('Test Merge w/same timestamp', () => { | ||
vi.useFakeTimers(); | ||
const date = new Date(2000,1,1,13); | ||
vi.setSystemTime(date); | ||
|
||
let register1 = new LWWRegister<string>("alice", "node1"); | ||
let register2 = new LWWRegister<string>("bob", "node2"); | ||
|
||
expect(register1.getElement()).toBe("alice"); | ||
expect(register1.getNodeId()).toEqual("node1"); | ||
|
||
register1.merge(register2); | ||
expect(register1.getElement()).toBe("bob"); | ||
expect(register1.getNodeId()).toEqual("node2"); | ||
|
||
register2.merge(register1); | ||
expect(register1.getElement()).toBe("bob"); | ||
expect(register1.getNodeId()).toEqual("node2"); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.