forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
request/nonce: Refactor to simplify package and prevent consecutive m…
…utex lock calls when accessing/setting nonce values (thrasher-corp#1506) * improv. timed mutex * Add all protection back in and jankyness because races. :'( * Add intial benchmarkeroos * Add master benchmarks * goodness me * what? * what again? * glorious: nits * just a swaperino instead * clean up package nonce so that we only need to aquire mutex once * unlock before checking master * commentary * wha * more comment * ch comment * nonce: Allow for broad customisation externally with a ~2ns overhead * glorious: nits maybe works? --------- Co-authored-by: Ryan O'Hara-Reid <[email protected]>
- Loading branch information
Showing
12 changed files
with
95 additions
and
155 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
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 |
---|---|---|
@@ -1,62 +1,36 @@ | ||
package nonce | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
var nonce Nonce | ||
nonce.Set(112321313) | ||
if expected, result := Value(112321313), nonce.Get(); expected != result { | ||
t.Errorf("Expected %d got %d", expected, result) | ||
} | ||
} | ||
|
||
func TestGetInc(t *testing.T) { | ||
var nonce Nonce | ||
nonce.Set(1) | ||
if expected, result := Value(2), nonce.GetInc(); expected != result { | ||
t.Errorf("Expected %d got %d", expected, result) | ||
} | ||
} | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSet(t *testing.T) { | ||
func TestGetAndIncrement(t *testing.T) { | ||
var nonce Nonce | ||
nonce.Set(1) | ||
if result, expected := nonce.Get(), Value(1); expected != result { | ||
t.Errorf("Expected %d got %d", expected, result) | ||
} | ||
n1 := nonce.GetAndIncrement(Unix) | ||
assert.NotZero(t, n1) | ||
n2 := nonce.GetAndIncrement(Unix) | ||
assert.NotZero(t, n2) | ||
assert.NotEqual(t, n1, n2) | ||
|
||
var nonce2 Nonce | ||
n3 := nonce2.GetAndIncrement(UnixNano) | ||
assert.NotZero(t, n3) | ||
n4 := nonce2.GetAndIncrement(UnixNano) | ||
assert.NotZero(t, n4) | ||
assert.NotEqual(t, n3, n4) | ||
|
||
assert.NotEqual(t, n1, n3) | ||
assert.NotEqual(t, n2, n4) | ||
} | ||
|
||
func TestString(t *testing.T) { | ||
var nonce Nonce | ||
nonce.Set(12312313131) | ||
expected := "12312313131" | ||
result := nonce.String() | ||
if expected != result { | ||
t.Errorf("Expected %s got %s", expected, result) | ||
} | ||
|
||
v := nonce.Get() | ||
if expected != v.String() { | ||
t.Errorf("Expected %s got %s", expected, result) | ||
} | ||
} | ||
|
||
func TestNonceConcurrency(t *testing.T) { | ||
var nonce Nonce | ||
nonce.Set(12312) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1000) | ||
for i := 0; i < 1000; i++ { | ||
go func() { nonce.GetInc(); wg.Done() }() | ||
} | ||
|
||
wg.Wait() | ||
nonce.n = 12312313131 | ||
got := nonce.GetAndIncrement(Unix) | ||
assert.Equal(t, "12312313131", got.String()) | ||
|
||
if expected, result := Value(12312+1000), nonce.Get(); expected != result { | ||
t.Errorf("Expected %d got %d", expected, result) | ||
} | ||
got = nonce.GetAndIncrement(Unix) | ||
assert.Equal(t, "12312313132", got.String()) | ||
} |
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.