-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added load and store functions for persistence with tests
- Loading branch information
1 parent
c0c8d0b
commit abc165c
Showing
4 changed files
with
137 additions
and
3 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
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,85 @@ | ||
package com.tap.hlc | ||
|
||
import com.benasher44.uuid.uuid4 | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import okio.Path.Companion.toPath | ||
import okio.fakefilesystem.FakeFileSystem | ||
|
||
class HLCPersistTest { | ||
|
||
@Test | ||
fun `can store the hlc into a file at a given path`() { | ||
|
||
val fileSystem = FakeFileSystem() | ||
|
||
val epochMillis = 943920000000L | ||
val counter = 15 | ||
val node = uuid4() | ||
|
||
val clock = HybridLogicalClock(Timestamp(epochMillis), NodeID.mint(node), counter) | ||
val path = "/Users/alice".toPath() | ||
val filename = "test.hlc" | ||
|
||
HybridLogicalClock.store(clock, path, fileSystem, filename) | ||
|
||
val expectedEncoded = "${epochMillis.toString().padStart(15, '0')}:${counter.toString(36).padStart(5, '0')}:${node.toString().replace("-", "").takeLast(16)}" | ||
val result = fileSystem.read(path / filename) { | ||
readUtf8() | ||
} | ||
|
||
assertEquals(expectedEncoded, result) | ||
fileSystem.checkNoOpenFiles() | ||
} | ||
|
||
@Test | ||
fun `can load a hlc from a given path`() { | ||
|
||
val fileSystem = FakeFileSystem() | ||
val path = "/Users/alice".toPath() | ||
fileSystem.createDirectories(path) | ||
val filename = "test.hlc" | ||
|
||
val epochMillis = 943920000000L | ||
val counter = 15 | ||
val node = uuid4().toString().replace("-", "").takeLast(16) | ||
|
||
val encoded = "${epochMillis.toString().padStart(15, '0')}:${counter.toString(36).padStart(5, '0')}:$node" | ||
|
||
fileSystem.write(path / filename) { | ||
writeUtf8(encoded) | ||
} | ||
|
||
val result = HybridLogicalClock.load(path, fileSystem, filename) | ||
|
||
assertNotNull(result) | ||
assertEquals(result.timestamp.epochMillis, epochMillis) | ||
assertEquals(result.counter, counter) | ||
assertEquals(result.node.identifier, node) | ||
fileSystem.checkNoOpenFiles() | ||
} | ||
|
||
@Test | ||
fun `can store and load a hlc to and from a given path`() { | ||
|
||
val fileSystem = FakeFileSystem() | ||
val path = "/Users/alice".toPath() | ||
fileSystem.createDirectories(path) | ||
val filename = "test.hlc" | ||
|
||
val epochMillis = 943920000000L | ||
val counter = 15 | ||
val node = uuid4() | ||
|
||
val clock = HybridLogicalClock(Timestamp(epochMillis), NodeID.mint(node), counter) | ||
HybridLogicalClock.store(clock, path, fileSystem, filename) | ||
val result = HybridLogicalClock.load(path, fileSystem, filename) | ||
|
||
assertNotNull(result) | ||
assertEquals(result.timestamp.epochMillis, epochMillis) | ||
assertEquals(result.counter, counter) | ||
assertEquals(result.node.identifier, NodeID.mint(node).identifier) | ||
fileSystem.checkNoOpenFiles() | ||
} | ||
} |