-
Notifications
You must be signed in to change notification settings - Fork 3
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
Skip unnecessary writes in default impl for modifyGet #70
Open
tbekas
wants to merge
2
commits into
master
Choose a base branch
from
skip-unnecessary-write
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,95 @@ | ||
import std/options | ||
import std/sequtils | ||
import std/tables | ||
|
||
import pkg/asynctest/chronos/unittest2 | ||
import pkg/chronos | ||
import pkg/questionable | ||
import pkg/questionable/results | ||
|
||
import pkg/datastore | ||
import pkg/datastore/defaultimpl | ||
|
||
type | ||
MockDatastore* = ref object of Datastore | ||
values: TableRef[Key, seq[byte]] | ||
lock: AsyncLock | ||
calls: seq[MethodCall] | ||
|
||
Method = enum | ||
Put, Get, Delete, Has | ||
|
||
MethodCall = object | ||
key: Key | ||
case kind: Method | ||
of Put: | ||
data: seq[byte] | ||
of Get, Delete, Has: | ||
discard | ||
|
||
method put*(self: MockDatastore, key: Key, data: seq[byte]): Future[?!void] {.async.} = | ||
self.calls.add(MethodCall(kind: Put, key: key, data: data)) | ||
self.values[key] = data | ||
success() | ||
|
||
method get*(self: MockDatastore, key: Key): Future[?!seq[byte]] {.async.} = | ||
self.calls.add(MethodCall(kind: Get, key: key)) | ||
if key notin self.values: | ||
failure(newException(DatastoreKeyNotFound, "Key doesn't exist")) | ||
else: | ||
success(self.values[key]) | ||
|
||
method has*(self: MockDatastore, key: Key): Future[?!bool] {.async.} = | ||
self.calls.add(MethodCall(kind: Has, key: key)) | ||
success(key in self.values) | ||
|
||
method delete*(self: MockDatastore, key: Key): Future[?!void] {.async.} = | ||
self.calls.add(MethodCall(kind: Delete, key: key)) | ||
self.values.del(key) | ||
success() | ||
|
||
method modifyGet*(self: MockDatastore, key: Key, fn: ModifyGet): Future[?!seq[byte]] {.async.} = | ||
await defaultModifyGetImpl(self, self.lock, key, fn) | ||
|
||
method modify*(self: MockDatastore, key: Key, fn: Modify): Future[?!void] {.async.} = | ||
await defaultModifyImpl(self, self.lock, key, fn) | ||
|
||
proc new*( | ||
T: type MockDatastore): T = | ||
T( | ||
values: newTable[Key, seq[byte]](), | ||
lock: newAsyncLock(), | ||
calls: newSeq[MethodCall]() | ||
) | ||
|
||
suite "Test defaultimpl": | ||
var | ||
ds: MockDatastore | ||
key: Key | ||
|
||
setup: | ||
ds = MockDatastore.new() | ||
key = Key.init("/a/b").tryGet() | ||
|
||
test "should put a value that is different than the current value": | ||
|
||
(await ds.put(key, @[byte 1, 2, 3])).tryGet() | ||
|
||
proc modifyFn(maybeCurr: ?seq[byte]): Future[?seq[byte]] {.async.} = | ||
some(@[byte 3, 2, 1]) | ||
|
||
(await ds.modify(key, modifyFn)).tryGet() | ||
|
||
check: | ||
ds.calls.filterIt(it.kind == Put).len == 2 | ||
|
||
test "should not attempt to put a value that is equal to the current value": | ||
(await ds.put(key, @[byte 1, 2, 3])).tryGet() | ||
|
||
proc modifyFn(maybeCurr: ?seq[byte]): Future[?seq[byte]] {.async.} = | ||
some(@[byte 1, 2, 3]) | ||
|
||
(await ds.modify(key, modifyFn)).tryGet() | ||
|
||
check: | ||
ds.calls.filterIt(it.kind == Put).len == 1 |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 Well technically you should be able to test for this (the absence of a write), and I'd argue you should test for it as otherwise you can't know if your optimization is actually working (which is something we care about, as otherwise there'd be no point in putting it there), no matter how convincing the code looks. 🙂
I'm wiling to let this pass - against my own best judgement - if you tell me that writing such a test is complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that skipping writes is not such a crucial feature that would justify having a test that verify if something was called or not called (one of the worst kinds of tests imo), but I've added such test anyway.