test: Test cases for lamport clock and oplog sorting. (#28)

* test: Test cases for lamport clock and oplog sorting.

* fix: Linting.
This commit is contained in:
Hayden Young 2023-03-06 04:45:15 +08:00 committed by GitHub
parent c684673ae1
commit 0347f3a7c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 357 additions and 0 deletions

View File

@ -0,0 +1,172 @@
import Clock from '../../src/oplog/lamport-clock.js'
import { strictEqual } from 'assert'
describe('Lamport Clock', () => {
it('creates a new clock', () => {
const id = 'A'
const time = 0
const clock = new Clock(id, time)
strictEqual(clock.id, id)
strictEqual(clock.time, time)
})
it('creates a new clock with default time', () => {
const id = 'A'
const time = 0
const clock = new Clock(id)
strictEqual(clock.id, id)
strictEqual(clock.time, time)
})
it('creates a new clock with time starting at 1', () => {
const id = 'A'
const time = 1
const clock = new Clock(id, time)
strictEqual(clock.id, id)
strictEqual(clock.time, time)
})
it('advances clock forward 1 tick', () => {
const id = 'A'
const time = 1
const clock = new Clock(id)
clock.tick()
strictEqual(clock.time, time)
})
it('advances clock forward 2 ticks', () => {
const id = 'A'
const time = 2
const clock = new Clock(id)
clock.tick()
clock.tick()
strictEqual(clock.time, time)
})
it('merges clock2 into clock1', () => {
const id1 = 'A'
const clock1 = new Clock(id1)
const id2 = 'B'
const time2 = 2
const clock2 = new Clock(id2, time2)
clock1.merge(clock2)
strictEqual(clock1.id, id1)
strictEqual(clock1.time, time2)
})
it('merges two clocks with the same time', () => {
const id1 = 'A'
const time1 = 1
const clock1 = new Clock(id1, time1)
const id2 = 'B'
const time2 = 1
const clock2 = new Clock(id2, time2)
clock1.merge(clock2)
strictEqual(clock1.id, id1)
strictEqual(clock1.time, time1)
})
it('clones a clock', () => {
const id = 'A'
const time = 1
const clock = new Clock(id, time)
const clonedClock = clock.clone()
strictEqual(clonedClock.id, id)
strictEqual(clonedClock.time, time)
})
describe('Compare clocks', () => {
it('compares clocks when clock1\'s time is 1 less than clock2\'s', () => {
const id1 = 'A'
const time1 = 1
const clock1 = new Clock(id1, time1)
const id2 = 'B'
const time2 = 2
const clock2 = new Clock(id2, time2)
const expected = -1
strictEqual(Clock.compare(clock1, clock2), expected)
})
it('compares clocks when clock1\'s time is 3 less than clock2\'s', () => {
const id1 = 'A'
const time1 = 1
const clock1 = new Clock(id1, time1)
const id2 = 'B'
const time2 = 4
const clock2 = new Clock(id2, time2)
const expected = -3
strictEqual(Clock.compare(clock1, clock2), expected)
})
it('compares clocks when clock1\'s time is 1 more than clock2\'s', () => {
const id1 = 'A'
const time1 = 2
const clock1 = new Clock(id1, time1)
const id2 = 'B'
const time2 = 1
const clock2 = new Clock(id2, time2)
const expected = 1
strictEqual(Clock.compare(clock1, clock2), expected)
})
it('compares clocks when clock1\'s time is 3 more than clock2\'s', () => {
const id1 = 'A'
const time1 = 4
const clock1 = new Clock(id1, time1)
const id2 = 'B'
const time2 = 1
const clock2 = new Clock(id2, time2)
const expected = 3
strictEqual(Clock.compare(clock1, clock2), expected)
})
it('compares clocks when clock1\'s id is less than clock2\'s', () => {
const id1 = 'A'
const time1 = 1
const clock1 = new Clock(id1, time1)
const id2 = 'B'
const time2 = 1
const clock2 = new Clock(id2, time2)
const expected = -1
strictEqual(Clock.compare(clock1, clock2), expected)
})
it('compares clocks when clock1\'s id is more than clock2\'s', () => {
const id1 = 'B'
const time1 = 1
const clock1 = new Clock(id1, time1)
const id2 = 'A'
const time2 = 1
const clock2 = new Clock(id2, time2)
const expected = 1
strictEqual(Clock.compare(clock1, clock2), expected)
})
it('compares clocks when clock1 is the same as clock2', () => {
const id = 'A'
const time = 1
const clock1 = new Clock(id, time)
const clock2 = new Clock(id, time)
const expected = 0
strictEqual(Clock.compare(clock1, clock2), expected)
})
})
})

185
test/oplog/sorting.test.js Normal file
View File

@ -0,0 +1,185 @@
import { strictEqual, deepStrictEqual } from 'assert'
import Clock from '../../src/oplog/lamport-clock.js'
import Sorting from '../../src/oplog/sorting.js'
describe('Sorting', () => {
describe('NoZeroes', () => {
it('passed function cannot return 0', () => {
let err
const func = (a, b) => { return 0 }
const sortFn = Sorting.NoZeroes(func)
const expected = 'Error: Your log\'s tiebreaker function, func, has returned zero and therefore cannot be'
const record1 = 1
const record2 = 2
try {
sortFn(record1, record2)
} catch (e) {
err = e.toString()
}
strictEqual(err, expected)
})
})
describe('SortByClockId', () => {
let fallbackFn
before(() => {
fallbackFn = (a, b) => a
})
it('returns -1 when first clock\'s id is less than second clock\'s', () => {
const expected = -1
const record1 = { clock: new Clock('A') }
const record2 = { clock: new Clock('B') }
strictEqual(Sorting.SortByClockId(record1, record2, fallbackFn), expected)
})
it('returns 1 when first clock\'s id is greater than second clock\'s', () => {
const expected = 1
const record1 = { clock: new Clock('B') }
const record2 = { clock: new Clock('A') }
strictEqual(Sorting.SortByClockId(record1, record2, fallbackFn), expected)
})
it('returns the clock when clocks have the same id', () => {
const expected = { clock: new Clock('A') }
const record1 = { clock: new Clock('A') }
const record2 = { clock: new Clock('A') }
deepStrictEqual(Sorting.SortByClockId(record1, record2, fallbackFn), expected)
})
})
describe('SortByClocks', () => {
let fallbackFn
before(() => {
fallbackFn = (a, b) => a
})
it('returns -1 when a\'s time is less than b\'s', () => {
const expected = -1
const record1 = { clock: new Clock('A', 1) }
const record2 = { clock: new Clock('B', 2) }
strictEqual(Sorting.SortByClocks(record1, record2, fallbackFn), expected)
})
it('returns 1 when a\'s time is greater than b\'s', () => {
const expected = 1
const record1 = { clock: new Clock('A', 2) }
const record2 = { clock: new Clock('B', 1) }
strictEqual(Sorting.SortByClocks(record1, record2, fallbackFn), expected)
})
it('returns -1 when a\'s time is equal to b\'s', () => {
const expected = -1
const record1 = { clock: new Clock('A', 1) }
const record2 = { clock: new Clock('B', 1) }
strictEqual(Sorting.SortByClocks(record1, record2, fallbackFn), expected)
})
})
describe('Last write wins', () => {
it('returns -1 when a\'s time is less than b\'s', () => {
const expected = -1
const record1 = { clock: new Clock('A', 1) }
const record2 = { clock: new Clock('B', 2) }
strictEqual(Sorting.LastWriteWins(record1, record2), expected)
})
it('returns 1 when a\'s time is greater than b\'s', () => {
const expected = 1
const record1 = { clock: new Clock('A', 2) }
const record2 = { clock: new Clock('B', 1) }
strictEqual(Sorting.LastWriteWins(record1, record2), expected)
})
it('returns -1 when a\'s time is equal to b\'s', () => {
const expected = -1
const record1 = { clock: new Clock('A', 1) }
const record2 = { clock: new Clock('B', 1) }
strictEqual(Sorting.LastWriteWins(record1, record2), expected)
})
it('returns the clock when a and b are the same', () => {
const expected = { clock: new Clock('A') }
const record1 = { clock: new Clock('A') }
const record2 = { clock: new Clock('A') }
deepStrictEqual(Sorting.LastWriteWins(record1, record2), expected)
})
})
describe('Sorting records', () => {
it('sorts by clock time', () => {
const expected = [
{ clock: new Clock('A', 1) },
{ clock: new Clock('B', 2) },
{ clock: new Clock('C', 3) },
{ clock: new Clock('D', 4) }
]
const records = [
{ clock: new Clock('C', 3) },
{ clock: new Clock('A', 1) },
{ clock: new Clock('D', 4) },
{ clock: new Clock('B', 2) }
]
deepStrictEqual(records.sort(Sorting.LastWriteWins), expected)
})
it('sorts by clock time when id is the same', () => {
const expected = [
{ clock: new Clock('A', 1) },
{ clock: new Clock('A', 2) },
{ clock: new Clock('A', 3) },
{ clock: new Clock('A', 4) }
]
const records = [
{ clock: new Clock('A', 3) },
{ clock: new Clock('A', 1) },
{ clock: new Clock('A', 4) },
{ clock: new Clock('A', 2) }
]
deepStrictEqual(records.sort(Sorting.LastWriteWins), expected)
})
it('sorts by clock id', () => {
const expected = [
{ clock: new Clock('A') },
{ clock: new Clock('B') },
{ clock: new Clock('C') },
{ clock: new Clock('D') }
]
const records = [
{ clock: new Clock('C') },
{ clock: new Clock('A') },
{ clock: new Clock('D') },
{ clock: new Clock('B') }
]
deepStrictEqual(records.sort(Sorting.LastWriteWins), expected)
})
it('sorts the same clock', () => {
const expected = [
{ clock: new Clock('A') },
{ clock: new Clock('A') },
{ clock: new Clock('B') },
{ clock: new Clock('B') }
]
const records = [
{ clock: new Clock('B') },
{ clock: new Clock('A') },
{ clock: new Clock('B') },
{ clock: new Clock('A') }
]
deepStrictEqual(records.sort(Sorting.LastWriteWins), expected)
})
})
})