diff options
author | EuAndreh <eu@euandre.org> | 2025-01-23 09:17:46 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-01-23 09:17:46 -0300 |
commit | 08f74a09ebe0759d373019b4693df69c082367d0 (patch) | |
tree | f579b94d3e8c8527ab41b795e5399580d2596dcb | |
parent | src/stm.go: Rename AtomicGet => Deref and AtomicModify => Swap (diff) | |
download | stm-08f74a09ebe0759d373019b4693df69c082367d0.tar.gz stm-08f74a09ebe0759d373019b4693df69c082367d0.tar.xz |
tests/stm.go: Turn example into functional test
-rw-r--r-- | deps.mk | 9 | ||||
l--------- | tests/functional/usage/main.go | 1 | ||||
-rw-r--r-- | tests/functional/usage/stm.go | 76 | ||||
-rw-r--r-- | tests/stm.go | 72 |
4 files changed, 86 insertions, 72 deletions
@@ -2,20 +2,24 @@ libs.go = \ src/stm.go \ tests/benchmarks/atomic-set/stm.go \ tests/functional/santa-claus/stm.go \ + tests/functional/usage/stm.go \ tests/fuzz/api/stm.go \ tests/stm.go \ mains.go = \ tests/benchmarks/atomic-set/main.go \ tests/functional/santa-claus/main.go \ + tests/functional/usage/main.go \ tests/fuzz/api/main.go \ tests/main.go \ functional-tests/lib.go = \ tests/functional/santa-claus/stm.go \ + tests/functional/usage/stm.go \ functional-tests/main.go = \ tests/functional/santa-claus/main.go \ + tests/functional/usage/main.go \ fuzz-targets/lib.go = \ tests/fuzz/api/stm.go \ @@ -34,19 +38,24 @@ tests/benchmarks/atomic-set/main.a: tests/benchmarks/atomic-set/main.go tests/benchmarks/atomic-set/stm.a: tests/benchmarks/atomic-set/stm.go tests/functional/santa-claus/main.a: tests/functional/santa-claus/main.go tests/functional/santa-claus/stm.a: tests/functional/santa-claus/stm.go +tests/functional/usage/main.a: tests/functional/usage/main.go +tests/functional/usage/stm.a: tests/functional/usage/stm.go tests/fuzz/api/main.a: tests/fuzz/api/main.go tests/fuzz/api/stm.a: tests/fuzz/api/stm.go tests/main.a: tests/main.go tests/stm.a: tests/stm.go tests/benchmarks/atomic-set/main.bin: tests/benchmarks/atomic-set/main.a tests/functional/santa-claus/main.bin: tests/functional/santa-claus/main.a +tests/functional/usage/main.bin: tests/functional/usage/main.a tests/fuzz/api/main.bin: tests/fuzz/api/main.a tests/main.bin: tests/main.a tests/benchmarks/atomic-set/main.bin-check: tests/benchmarks/atomic-set/main.bin tests/functional/santa-claus/main.bin-check: tests/functional/santa-claus/main.bin +tests/functional/usage/main.bin-check: tests/functional/usage/main.bin tests/fuzz/api/main.bin-check: tests/fuzz/api/main.bin tests/main.bin-check: tests/main.bin tests/benchmarks/atomic-set/main.a: tests/benchmarks/atomic-set/$(NAME).a tests/functional/santa-claus/main.a: tests/functional/santa-claus/$(NAME).a +tests/functional/usage/main.a: tests/functional/usage/$(NAME).a tests/fuzz/api/main.a: tests/fuzz/api/$(NAME).a tests/main.a: tests/$(NAME).a diff --git a/tests/functional/usage/main.go b/tests/functional/usage/main.go new file mode 120000 index 0000000..f67563d --- /dev/null +++ b/tests/functional/usage/main.go @@ -0,0 +1 @@ +../../main.go
\ No newline at end of file diff --git a/tests/functional/usage/stm.go b/tests/functional/usage/stm.go new file mode 100644 index 0000000..dcc6736 --- /dev/null +++ b/tests/functional/usage/stm.go @@ -0,0 +1,76 @@ +package stm + +import ( + g "gobang" +) + + + +func MainTest() { + g.TestStart("Example of API usage") + + g.Testing("operating on int variables", func() { + // create a shared variable + n := NewVar(3) + + // read a variable + var v int + Atomically(VoidOperation(func(tx *Tx) { + v = n.Get(tx) + })) + g.TAssertEqual(v, 3) + + // or: + v = Deref(n) + g.TAssertEqual(v, 3) + + // write to a variable + Atomically(VoidOperation(func(tx *Tx) { + n.Set(tx, 12) + })) + g.TAssertEqual(Deref(n), 12) + + // or: + AtomicSet(n, 11) + g.TAssertEqual(Deref(n), 11) + + // update a variable + Atomically(VoidOperation(func(tx *Tx) { + cur := n.Get(tx) + n.Set(tx, cur+1) + })) + g.TAssertEqual(Deref(n), 12) + + // block until a condition is met + Atomically(VoidOperation(func(tx *Tx) { + cur := n.Get(tx) + if cur == 0 { + tx.Retry() + } + n.Set(tx, 10) + })) + g.TAssertEqual(Deref(n), 10) + + // or: + Atomically(VoidOperation(func(tx *Tx) { + cur := n.Get(tx) + tx.Assert(cur != 0) + n.Set(tx, 11) + })) + g.TAssertEqual(Deref(n), 11) + + // select among multiple (potentially blocking) transactions + Atomically(Select( + // this function blocks forever, so it will be skipped + VoidOperation(func(tx *Tx) { tx.Retry() }), + + // this function will always succeed without blocking + VoidOperation(func(tx *Tx) { n.Set(tx, 10) }), + + // this function will never run, because the previous + // function succeeded + VoidOperation(func(tx *Tx) { n.Set(tx, 11) }), + )) + g.TAssertEqual(Deref(n), 10) + }) +} diff --git a/tests/stm.go b/tests/stm.go index ab05d5c..49fcd25 100644 --- a/tests/stm.go +++ b/tests/stm.go @@ -226,78 +226,6 @@ func BenchmarkPingPong(b *testing.B) { parallelPingPongs(b, 1) } -func Example() { - // create a shared variable - n := NewVar(3) - - // read a variable - var v int - Atomically(VoidOperation(func(tx *Tx) { - v = n.Get(tx) - })) - // or: - v = Deref(n) - _ = v - - // write to a variable - Atomically(VoidOperation(func(tx *Tx) { - n.Set(tx, 12) - })) - // or: - AtomicSet(n, 12) - - // update a variable - Atomically(VoidOperation(func(tx *Tx) { - cur := n.Get(tx) - n.Set(tx, cur-1) - })) - - // block until a condition is met - Atomically(VoidOperation(func(tx *Tx) { - cur := n.Get(tx) - if cur != 0 { - tx.Retry() - } - n.Set(tx, 10) - })) - // or: - Atomically(VoidOperation(func(tx *Tx) { - cur := n.Get(tx) - tx.Assert(cur == 0) - n.Set(tx, 10) - })) - - // select among multiple (potentially blocking) transactions - Atomically(Select( - // this function blocks forever, so it will be skipped - VoidOperation(func(tx *Tx) { tx.Retry() }), - - // this function will always succeed without blocking - VoidOperation(func(tx *Tx) { n.Set(tx, 10) }), - - // this function will never run, because the previous - // function succeeded - VoidOperation(func(tx *Tx) { n.Set(tx, 11) }), - )) - - // since Select is a normal transaction, if the entire select retries - // (blocks), it will be retried as a whole: - x := 0 - Atomically(Select( - // this function will run twice, and succeed the second time - VoidOperation(func(tx *Tx) { tx.Assert(x == 1) }), - - // this function will run once - VoidOperation(func(tx *Tx) { - x = 1 - tx.Retry() - }), - )) - // But wait! Transactions are only retried when one of the Vars they read is - // updated. Since x isn't a stm Var, this code will actually block forever -- - // but you get the idea. -} - const maxTokens = 25 func BenchmarkThunderingHerdCondVar(b *testing.B) { |