Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dave committed Oct 22, 2017
1 parent 644b6dd commit 487724b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
36 changes: 20 additions & 16 deletions blaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
func TestNew(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

newBlast := func(in string, logbuf *bytes.Buffer) (*Blaster, *bytes.Buffer) {
newBlast := func(in string, workerType string, logbuf *bytes.Buffer) (*Blaster, *bytes.Buffer) {
b := New(ctx, cancel)
b.RegisterWorkerType("success", NewImmediateSuccessWorker)
b.RegisterWorkerType("fail", NewImmediateFailWorker)

b.config = &configDef{}
b.config.Workers = 1
b.config.WorkerType = "success"
b.config.WorkerType = workerType
b.config.PayloadVariants = []map[string]string{{}}
b.rate = 100
b.dataHeaders = []string{"head"}
b.dataReader = csv.NewReader(strings.NewReader(in))
Expand All @@ -33,12 +33,8 @@ func TestNew(t *testing.T) {
logbuf := new(bytes.Buffer)
logbuf.WriteString("hash,result\n")

b, outbuf := newBlast("a\nb\nc", logbuf)

if err := b.start(ctx); err != nil {
t.Fatal(err)
}

b, outbuf := newBlast("a\nb\nc", "success", logbuf)
must(t, b.start(ctx))
b.logWriter.Flush()

mustMatch(t, outbuf, 1, `\nSuccess\:\s+0\n`)
Expand All @@ -47,24 +43,32 @@ func TestNew(t *testing.T) {
mustMatch(t, logbuf, 1, `db7a669e37739bf\|b4a36ba02942a475\,true\n`)
mustMatch(t, logbuf, 1, `deb69562b047222\|3cec67420f8a6588\,true\n`)

b1, outbuf1 := newBlast("a\nb\nc\nd", logbuf)
b1, outbuf1 := newBlast("a\nb\nc\nd", "success", logbuf)

if err := b1.loadPreviousLogsFromReader(bytes.NewBuffer(logbuf.Bytes())); err != nil {
t.Fatal(err)
}
if err := b1.start(ctx); err != nil {
t.Fatal(err)
}
must(t, b1.loadPreviousLogsFromReader(bytes.NewBuffer(logbuf.Bytes())))
must(t, b1.start(ctx))
b1.logWriter.Flush()

mustMatch(t, outbuf1, 1, `\nSuccess\:\s+0\n`)
mustMatch(t, outbuf1, 1, `\nSuccess\:\s+1\n`)
mustMatch(t, outbuf1, 1, `\nSkipped\:\s+3 \(from previous run\)\n`)
mustMatch(t, logbuf, 1, `73d81ec7b7251e65\|fab9096e8c84809f\,true\n`)

b2, outbuf2 := newBlast("e", "fail", logbuf)
must(t, b2.start(ctx))
b2.logWriter.Flush()
mustMatch(t, outbuf2, 1, `\nFailed\:\s+1\n`)

}

func must(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}

func mustMatch(t *testing.T, buf *bytes.Buffer, num int, pattern string) {
t.Helper()
matches := regexp.MustCompile(pattern).FindAllString(buf.String(), -1)
if len(matches) != num {
t.Fatalf("Matches in output (%d) not expected (%d) for pattern %s:\n%s",
Expand Down
3 changes: 3 additions & 0 deletions loop-worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func (b *Blaster) startWorkers(ctx context.Context) {
atomic.AddUint64(&b.stats.itemsStarted, 1)

success := true
if len(b.config.PayloadVariants) == 0 {
panic("no payload variants")
}
for _, variationData := range b.config.PayloadVariants {
atomic.AddUint64(&b.stats.requestsStarted, 1)
start := time.Now()
Expand Down

0 comments on commit 487724b

Please sign in to comment.