-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(test): refactor helpers into common files
Move unittest and functional test shared code into helper files Signed-off-by: Dominic Evans <[email protected]>
- Loading branch information
Showing
7 changed files
with
262 additions
and
240 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
//go:build go1.18 | ||
//go:build go1.18 && !functional | ||
|
||
package sarama | ||
|
||
|
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,87 @@ | ||
package sarama | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func safeClose(t testing.TB, c io.Closer) { | ||
t.Helper() | ||
err := c.Close() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func closeProducerWithTimeout(t *testing.T, p AsyncProducer, timeout time.Duration) { | ||
var wg sync.WaitGroup | ||
p.AsyncClose() | ||
|
||
closer := make(chan struct{}) | ||
timer := time.AfterFunc(timeout, func() { | ||
t.Error("timeout") | ||
close(closer) | ||
}) | ||
defer timer.Stop() | ||
|
||
wg.Add(2) | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case <-closer: | ||
return | ||
case _, ok := <-p.Successes(): | ||
if !ok { | ||
return | ||
} | ||
t.Error("Unexpected message on Successes()") | ||
} | ||
} | ||
}() | ||
go func() { | ||
defer wg.Done() | ||
for { | ||
select { | ||
case <-closer: | ||
return | ||
case msg, ok := <-p.Errors(): | ||
if !ok { | ||
return | ||
} | ||
t.Error(msg.Err) | ||
} | ||
} | ||
}() | ||
wg.Wait() | ||
} | ||
|
||
func closeProducer(t *testing.T, p AsyncProducer) { | ||
closeProducerWithTimeout(t, p, 5*time.Minute) | ||
} | ||
|
||
const TestMessage = "ABC THE MESSAGE" | ||
|
||
type appendInterceptor struct { | ||
i int | ||
} | ||
|
||
func (b *appendInterceptor) OnSend(msg *ProducerMessage) { | ||
if b.i < 0 { | ||
panic("hey, the interceptor has failed") | ||
} | ||
v, _ := msg.Value.Encode() | ||
msg.Value = StringEncoder(string(v) + strconv.Itoa(b.i)) | ||
b.i++ | ||
} | ||
|
||
func (b *appendInterceptor) OnConsume(msg *ConsumerMessage) { | ||
if b.i < 0 { | ||
panic("hey, the interceptor has failed") | ||
} | ||
msg.Value = []byte(string(msg.Value) + strconv.Itoa(b.i)) | ||
b.i++ | ||
} |
Oops, something went wrong.