Skip to content

Commit

Permalink
testing: Make the tests less flakey
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 31, 2024
1 parent 4b27570 commit 474a323
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func main() {
receipt.Size = uint32(123)

// Close the message stream.
// Pass true to drop any queued messages,
// this is only relevant if DelayDelivery is enabled.
c.Close(false, receipt)
},
},
Expand All @@ -152,7 +154,7 @@ The server can generate an ETag for the messages. This is a hash of all message
To enable this:

1. Provide a `GetHasher` function to the [server options](https://pkg.go.dev/github.com/bep/execrpc#ServerOptions).
2. Have the `Receipt` implement the [ETagger](https://pkg.go.dev/github.com/bep/execrpc#ETagger) interface.
2. Have the `Receipt` implement the [TagProvider](https://pkg.go.dev/github.com/bep/execrpc#TagProvider) interface.

Note that there are three different optional E-interfaces for the `Receipt`:

Expand Down
7 changes: 7 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func TestExecTyped(t *testing.T) {
assertMessages(c, result, 1)
receipt := <-result.Receipt()
c.Assert(receipt.GetESize(), qt.Equals, uint32(123))
c.Assert(receipt.ETag, qt.Equals, "2d5537627636b58a")
c.Assert(receipt.Text, qt.Equals, "echoed: world")
})

c.Run("100 messages", func(c *qt.C) {
Expand All @@ -126,6 +128,7 @@ func TestExecTyped(t *testing.T) {
receipt := <-result.Receipt()
c.Assert(receipt.LastModified, qt.Not(qt.Equals), int64(0))
c.Assert(receipt.ETag, qt.Equals, "15b8164b761923b7")
c.Assert(receipt.Text, qt.Equals, "echoed: world")
})

c.Run("1234 messages", func(c *qt.C) {
Expand All @@ -136,6 +139,7 @@ func TestExecTyped(t *testing.T) {
c.Assert(result.Err(), qt.IsNil)
c.Assert(receipt.LastModified, qt.Not(qt.Equals), int64(0))
c.Assert(receipt.ETag, qt.Equals, "43940b97841cc686")
c.Assert(receipt.Text, qt.Equals, "echoed: world")
})

c.Run("Delay delivery", func(c *qt.C) {
Expand All @@ -144,6 +148,7 @@ func TestExecTyped(t *testing.T) {
assertMessages(c, result, 1)
receipt := <-result.Receipt()
c.Assert(receipt.GetESize(), qt.Equals, uint32(123))
c.Assert(receipt.Text, qt.Equals, "echoed: world")
})

c.Run("Delay delivery, drop messages", func(c *qt.C) {
Expand All @@ -154,6 +159,8 @@ func TestExecTyped(t *testing.T) {
// This is a little confusing. We always get a receipt even if the messages are dropped,
// and the server can create whatever protocol it wants.
c.Assert(receipt.GetESize(), qt.Equals, uint32(123))
c.Assert(receipt.ETag, qt.Equals, "2d5537627636b58a")
c.Assert(receipt.Text, qt.Equals, "echoed: world")
})

c.Run("No Close", func(c *qt.C) {
Expand Down
16 changes: 0 additions & 16 deletions examples/servers/typed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"log"
"os"
"strconv"
"sync/atomic"
"time"

"github.com/bep/execrpc"
"github.com/bep/execrpc/examples/model"
Expand Down Expand Up @@ -101,23 +99,9 @@ func main() {
if !noClose {
var receipt model.ExampleReceipt
if !noReadingReceipt {
var receiptSeen atomic.Bool
go func() {
time.Sleep(1 * time.Second)
if !receiptSeen.Load() {
log.Fatalf("expected receipt to be seen")
}
}()

receipt = <-c.Receipt()
receipt.Text = "echoed: " + c.Request.Text
receipt.Size = uint32(123)

receiptSeen.Store(true)

if getHasher != nil && receipt.ETag == "" {
log.Fatalf("expected receipt eTag to be set")
}
}

c.Close(dropMessages, receipt)
Expand Down

0 comments on commit 474a323

Please sign in to comment.