From dccd98c578e8add2699fe1b8fee7ba48ee0c5744 Mon Sep 17 00:00:00 2001 From: Fedor Korotkiy Date: Thu, 19 Mar 2020 16:06:26 +0300 Subject: [PATCH] Fixes --- lectures/04-testing/lecture.slide | 43 ++++++++++++++++++++++++- lectures/04-testing/mocks/mocks.go | 29 +++++++++++++++++ lectures/04-testing/mocks/mocks_test.go | 26 +++++++++++++++ lectures/04-testing/race/race_test.go | 23 +++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 lectures/04-testing/mocks/mocks.go create mode 100644 lectures/04-testing/mocks/mocks_test.go create mode 100644 lectures/04-testing/race/race_test.go diff --git a/lectures/04-testing/lecture.slide b/lectures/04-testing/lecture.slide index 8b17ac6..19a663c 100644 --- a/lectures/04-testing/lecture.slide +++ b/lectures/04-testing/lecture.slide @@ -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/,/^}/ + + diff --git a/lectures/04-testing/mocks/mocks.go b/lectures/04-testing/mocks/mocks.go new file mode 100644 index 0000000..bf6d244 --- /dev/null +++ b/lectures/04-testing/mocks/mocks.go @@ -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 diff --git a/lectures/04-testing/mocks/mocks_test.go b/lectures/04-testing/mocks/mocks_test.go new file mode 100644 index 0000000..c3ebd49 --- /dev/null +++ b/lectures/04-testing/mocks/mocks_test.go @@ -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) + } +} diff --git a/lectures/04-testing/race/race_test.go b/lectures/04-testing/race/race_test.go new file mode 100644 index 0000000..0ed57c0 --- /dev/null +++ b/lectures/04-testing/race/race_test.go @@ -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 +}