-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Tests for all storage implementations - Tests basic functionality (read and write) as well as concurrency For #10
- Loading branch information
1 parent
2b0872b
commit 16dcdfc
Showing
5 changed files
with
237 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
sudo: required | ||
|
||
services: | ||
- redis-server | ||
- docker | ||
|
||
git: | ||
|
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,63 @@ | ||
package storage_test | ||
|
||
import ( | ||
"math/rand" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/philippgille/ln-paywall/wall" | ||
) | ||
|
||
// testStorageClient tests if reading and writing to the storage works properly. | ||
func testStorageClient(storageClient wall.StorageClient, t *testing.T) { | ||
key := strconv.FormatInt(rand.Int63(), 10) | ||
|
||
// Initially the key shouldn't exist, thus "was used" should be false | ||
expected := false | ||
actual, err := storageClient.WasUsed(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if actual != expected { | ||
t.Errorf("Expected: %v, but was: %v", expected, actual) | ||
} | ||
|
||
// Set the "key" to be used | ||
err = storageClient.SetUsed(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
// Check usage again, this time "was used" should be true | ||
expected = true | ||
actual, err = storageClient.WasUsed(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if actual != expected { | ||
t.Errorf("Expected: %v, but was: %v", expected, actual) | ||
} | ||
} | ||
|
||
// interactWithStorage reads from and writes to the DB. Meant to be executed in a goroutine. | ||
// Does NOT check if the DB works correctly (that's done by another test), only checks for errors | ||
func interactWithStorage(storageClient wall.StorageClient, key string, t *testing.T, waitGroup *sync.WaitGroup) { | ||
defer waitGroup.Done() | ||
|
||
// Read | ||
_, err := storageClient.WasUsed(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
// Write | ||
err = storageClient.SetUsed(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
// Read | ||
_, err = storageClient.WasUsed(key) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |