This commit is contained in:
Fedor Korotkiy 2020-03-19 16:06:26 +03:00
parent 8d5a33c1b7
commit dccd98c578
4 changed files with 120 additions and 1 deletions

View file

@ -241,6 +241,11 @@ Good example
* testify
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSum0(t *testing.T) {
assert.Equalf(t, 4, Sum(1, 2), "Sum(%d, %d)", 1, 2)
}
@ -334,7 +339,7 @@ Good example
// false
}
Если `Output` нет, то `Example` слушит только для документации.
Если `Output` нет, то `Example` служит только для документации.
func ExampleAPI() {
var c *Client // skip initialization
@ -378,6 +383,7 @@ Good example
* t.Helper()
func assertGood(t *testing.T, i int) {
t.Helper()
if i != 0 {
t.Errorf("i (= %d) != 0", i)
}
@ -446,3 +452,38 @@ Good example
}
}
* Race detector
.play race/race_test.go
* Race detector
prime@bee ~/C/shad-go> go test -race ./lectures/04-testing/race
==================
WARNING: DATA RACE
Read at 0x00c000092090 by goroutine 8:
gitlab.com/slon/shad-go/lectures/04-testing/race.TestRace()
/home/prime/Code/shad-go/lectures/04-testing/race/race_test.go:25 +0x144
testing.tRunner()
/usr/local/go/src/testing/testing.go:909 +0x199
Previous write at 0x00c000092090 by goroutine 9:
gitlab.com/slon/shad-go/lectures/04-testing/race.TestRace.func1()
/home/prime/Code/shad-go/lectures/04-testing/race/race_test.go:17 +0x6c
...
==================
--- FAIL: TestRace (0.00s)
testing.go:853: race detected during execution of test
FAIL
FAIL gitlab.com/slon/shad-go/lectures/04-testing/race 0.007s
* White box testing
.play mocks/mocks.go /var/,/OMIT/
* White box testing
.play mocks/mocks_test.go /func/,/^}/

View file

@ -0,0 +1,29 @@
import (
"fmt"
"log"
"net/smtp"
)
var notifyUser = doNotifyUser
func doNotifyUser(username, msg string) {
auth := smtp.PlainAuth("", sender, password, hostname)
err := smtp.SendMail(hostname+":587", auth, sender,
[]string{username}, []byte(msg))
if err != nil {
log.Printf("smtp.SendEmail(%s) failed: %s", username, err)
}
}
func CheckQuota(username string) {
used := bytesInUse(username)
const quota = 1000000000 // 1GB
percent := 100 * used / quota
if percent < 90 {
return // OK
}
msg := fmt.Sprintf(template, used, percent)
notifyUser(username, msg)
}
// OMIT

View file

@ -0,0 +1,26 @@
import (
"strings"
"testing"
)
func TestCheckQuotaNotifiesUser(t *testing.T) {
var notifiedUser, notifiedMsg string
notifyUser = func(user, msg string) {
notifiedUser, notifiedMsg = user, msg
}
// ...simulate a 980MB-used condition...
const user = "joe@example.org"
CheckQuota(user)
if notifiedUser == "" && notifiedMsg == "" {
t.Fatalf("notifyUser not called")
}
if notifiedUser != user {
t.Errorf("wrong user (%s) notified, want %s",
notifiedUser, user)
}
const wantSubstring = "98% of your quota"
if !strings.Contains(notifiedMsg, wantSubstring) {
t.Errorf("unexpected notification message <<%s>>, "+
"want substring %q", notifiedMsg, wantSubstring)
}
}

View file

@ -0,0 +1,23 @@
package race
import (
"sync"
"testing"
)
func TestRace(t *testing.T) {
var wg sync.WaitGroup
defer wg.Wait()
wg.Add(2)
var i int
go func() {
defer wg.Done()
i = 0
}()
go func() {
defer wg.Done()
i = 1
}()
_ = i
}