1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2025-06-12 21:47:29 +02:00

allow unordered expectation matching, support for goroutines

* 1778939 take care of locks for goroutine based matching
This commit is contained in:
gedi
2015-08-26 14:28:01 +03:00
parent 5a740a6373
commit 566ca54083
4 changed files with 276 additions and 103 deletions

View File

@ -3,6 +3,7 @@ package sqlmock
import (
"database/sql"
"fmt"
"sync"
"testing"
"time"
)
@ -575,3 +576,44 @@ func TestArgumentReflectValueTypeError(t *testing.T) {
t.Error("Expected error, but got none")
}
}
func TestGoroutineExecutionWithUnorderedExpectationMatching(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
// note this line is important for unordered expectation matching
mock.MatchExpectationsInOrder = false
result := NewResult(1, 1)
mock.ExpectExec("^UPDATE one").WithArgs("one").WillReturnResult(result)
mock.ExpectExec("^UPDATE two").WithArgs("one", "two").WillReturnResult(result)
mock.ExpectExec("^UPDATE three").WithArgs("one", "two", "three").WillReturnResult(result)
var wg sync.WaitGroup
queries := map[string][]interface{}{
"one": []interface{}{"one"},
"two": []interface{}{"one", "two"},
"three": []interface{}{"one", "two", "three"},
}
wg.Add(len(queries))
for table, args := range queries {
go func(tbl string, a []interface{}) {
if _, err := db.Exec("UPDATE "+tbl, a...); err != nil {
t.Errorf("error was not expected: %s", err)
}
wg.Done()
}(table, args)
}
wg.Wait()
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}