- 1. 好險(xiǎn),差點(diǎn)被噴
-
2. 重構(gòu)測試代碼
- 2.1 引入 Ginkgo 測試框架
- 2.2 嘗試改造舊的測試用例
- 2.3 重構(gòu)功能測試代碼
- 3. 總結(jié)
1. 好險(xiǎn),差點(diǎn)被噴
早幾天發(fā)了一篇文章:《僅三天,我用 GPT-4 生成了性能全網(wǎng)第一的 Golang Worker Pool,輕松打敗 GitHub 萬星項(xiàng)目》,這標(biāo)題是挺容易被懟,哇咔咔;不過最終“下場”不算慘,懟的聲音很少,相反很多開發(fā)者馬上就在自己的項(xiàng)目里開始使用 GoPool了,刺激!
等等,你以為“刺激”的只是“我”?咳咳,其實(shí),那些開發(fā)者也感覺很“刺激”。這不,他們被螃蟹殼塞牙了,你看 GoPool 的 issue 列表:
(容我辯解5秒鐘)Bugs 有歸有,不過平均一個(gè) issue 的解決時(shí)間沒有超過半天,至少我提供的售后服務(wù)還可以的,對吧?哈哈……
(5秒到了)不得不承認(rèn):一個(gè)足夠健壯的 Worker Pool 還是需要花點(diǎn)時(shí)間來打磨。
那就再打磨一下吧:
- 《讓 GPT-4 來修復(fù) Golang “數(shù)據(jù)競爭”問題》
- 《讓 GPT-4 來修復(fù) Golang “數(shù)據(jù)競爭”問題(續(xù))》
2. 重構(gòu)測試代碼
當(dāng)前所有測試用例都放在 gopool_test.go
中,這個(gè)文件已經(jīng)接近200行了,維護(hù)起來開始吃力。所以我決定今天先讓 GPT-4 來改造一下測試用例的組織方式。
下文使用 DevChat 和 GPT-4 交互。如果你對 DevChat 感興趣,可以跳轉(zhuǎn)閱讀這篇文章:
- 《DevChat:將 GPT-4 無縫融入 VS Code,極致提升你的編程體驗(yàn)》
- 如果你想要注冊 DevChat,點(diǎn)擊這個(gè)鏈接可以免費(fèi)注冊,目前 DevChat 還在公測中,不收費(fèi)。
2.1 引入 Ginkgo 測試框架
- 我對 DevChat 說:
當(dāng)前我的測試代碼全部在 gopool_test.go 中,我覺得可讀性不夠好。我希望你引入 Ginkgo 測試框架來幫我重新組織測試代碼。首先我只需要你告訴我如何引入 Ginkgo,搭建測試骨架代碼,而不用編寫具體的測試用例。
DevChat 的回答如下:
要引入 Ginkgo 測試框架并搭建測試骨架代碼,你需要按照以下步驟進(jìn)行:
- 首先,你需要安裝 Ginkgo 和 Gomega。Ginkgo 是一個(gè) Go 測試框架,Gomega 是一個(gè)配套的斷言庫。你可以通過以下命令安裝它們:
go get github.com/onsi/ginkgo/ginkgo
go get github.com/onsi/gomega/...
- 然后,你需要在你的測試目錄下生成一個(gè)新的 Ginkgo 測試套件。你可以通過以下命令生成:
ginkgo bootstrap
這將會生成一個(gè)名為 suite_test.go
的文件,其中包含了一個(gè)基本的測試套件骨架。
- 接下來,你可以為你的
gopool
包生成一個(gè) Ginkgo 測試文件。你可以通過以下命令生成:
ginkgo generate gopool
這將會生成一個(gè)名為 gopool_test.go
的文件,其中包含了一個(gè)基本的測試文件骨架。
- 最后,你可以在
gopool_test.go
文件中添加你的測試用例。以下是一個(gè)基本的測試用例骨架:
var _ = Describe("Gopool", func() {
Context("when something happens", func() {
It("should do something", func() {
// Your test code goes here
})
})
})
在這個(gè)骨架中,Describe
用于描述你正在測試的對象,Context
用于描述你正在測試的場景,It
用于描述你的期望結(jié)果。
以上就是引入 Ginkgo 并搭建測試骨架代碼的步驟。你可以根據(jù)你的需求修改和添加測試用例。
因?yàn)?gopool_test.go
文件已經(jīng)存在,Ginkgo 也沒有智能到能夠直接“merge”,所以我先將當(dāng)前代碼備份到了 gopool_1_test.go
中:
2.2 嘗試改造舊的測試用例
骨架有了,接下來我準(zhǔn)備嘗試讓 GPT-4 來重寫代碼。
- 我對 DevChat 說:
我將原先的測試用例到備份到了 gopool_1_test.go 中,現(xiàn)在我需要你用 Ginkgo 的最佳實(shí)踐重構(gòu)測試代碼,新的代碼將會放到 gopool_test.go 中。
DevChat 的回答如下:
首先,我們需要將原先的測試用例轉(zhuǎn)換為 Ginkgo 的格式。這包括將每個(gè)測試用例轉(zhuǎn)換為一個(gè) It
塊,并使用 Gomega 斷言庫來替換原先的斷言。以下是轉(zhuǎn)換后的測試代碼:
package gopool_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/devchat-ai/gopool"
)
var _ = Describe("Gopool", func() {
Describe("With Mutex", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
Describe("With SpinLock", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
Describe("With Error", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
Describe("With Result", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
Describe("With Retry", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
Describe("With Timeout", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
Describe("With MinWorkers", func() {
It("should work correctly", func() {
// Your test code goes here
})
})
})
這只是一個(gè)骨架代碼,你需要將原先的測試代碼復(fù)制到對應(yīng)的 It
塊中,并使用 Gomega 斷言庫來替換原先的斷言。例如,你可以將 t.Errorf
替換為 Expect().To(Equal())
。
對于基準(zhǔn)測試,Ginkgo 并不直接支持,但你可以使用 Go 的原生 testing
包來進(jìn)行基準(zhǔn)測試。你可以將基準(zhǔn)測試代碼放在一個(gè)單獨(dú)的文件中,例如 gopool_benchmark_test.go
。
啥?我感覺 GPT-4 偷懶了,居然不直接給我把代碼寫好。另外我沒記錯(cuò)的話,Ginkgo 是支持“基準(zhǔn)測試”的,可能是 GPT-4 的訓(xùn)練數(shù)據(jù)太舊導(dǎo)致它不知道這件事。(沒錯(cuò),這時(shí)候是它不是她。)
于是我先“笑納”了這段代碼,然后拆解問題,暫且將壓測代碼放到單獨(dú)的文件,讓 GPT-4 安心把功能測試代碼重構(gòu)好,然后我再和它算“基準(zhǔn)測試”的賬。
此刻我就有了一個(gè)新文件:
gopool_benchmark_test.go
里面內(nèi)容如下:
package gopool
import (
"sync"
"testing"
"time"
"github.com/daniel-hutao/spinlock"
)
func BenchmarkGoPoolWithMutex(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(1e6)
pool := NewGoPool(1e4, WithLock(new(sync.Mutex)))
defer pool.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(taskNum)
for num := 0; num < taskNum; num++ {
pool.AddTask(func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
wg.Done()
return nil, nil
})
}
wg.Wait()
}
b.StopTimer()
}
func BenchmarkGoPoolWithSpinLock(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(1e6)
pool := NewGoPool(1e4, WithLock(new(spinlock.SpinLock)))
defer pool.Release()
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(taskNum)
for num := 0; num < taskNum; num++ {
pool.AddTask(func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
wg.Done()
return nil, nil
})
}
wg.Wait()
}
b.StopTimer()
}
func BenchmarkGoroutines(b *testing.B) {
var wg sync.WaitGroup
var taskNum = int(1e6)
for i := 0; i < b.N; i++ {
wg.Add(taskNum)
for num := 0; num < taskNum; num++ {
go func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
wg.Done()
return nil, nil
}()
}
wg.Wait()
}
}
而 gopool_1_test.go
的內(nèi)容我就不贅述了,就是原先的全量測試代碼減去上面這些基準(zhǔn)測試代碼。那么接下來問題就拆分成了2個(gè):
- 用 Ginkgo 的方式改造功能測試代碼
- 用 Ginkgo 的方式改造基準(zhǔn)測試代碼
不過目測本文篇幅有點(diǎn)長了,所以我決定今天先完成功能測試代碼的改造,基準(zhǔn)測試下次再說。
2.3 重構(gòu)功能測試代碼
接下來該讓 GPT-4 開始重構(gòu)功能測試代碼了。
- 我對 DevChat 說:
幫我將 gopool_1_test.go 中的用 testing 庫編寫的測試代碼全部用 Ginkgo 的方式重構(gòu),更新到 gopool_test.go 中。
提示: DevChat 給出的代碼庫右上角有一個(gè)“diff”按鈕,點(diǎn)一下可以進(jìn)入“diff 視圖”,這樣就能看到當(dāng)前打開文件中的代碼和 GPT-4 給出的代碼之間的 diff,然后你就能清晰地看到 GPT-4 給出的代碼具體修改了什么了。
DevChat 的完整回答是:
好的,我將會按照你的要求,將 gopool_1_test.go
中的測試代碼用 Ginkgo 的方式重構(gòu),并更新到 gopool_test.go
中。
package gopool_test
import (
"errors"
"sync"
"sync/atomic"
"time"
"github.com/daniel-hutao/spinlock"
"github.com/devchat-ai/gopool"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Gopool", func() {
Describe("With Mutex", func() {
It("should work correctly", func() {
pool := gopool.NewGoPool(100, gopool.WithLock(new(sync.Mutex)))
defer pool.Release()
for i := 0; i < 1000; i++ {
pool.AddTask(func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
return nil, nil
})
}
pool.Wait()
})
})
Describe("With SpinLock", func() {
It("should work correctly", func() {
pool := gopool.NewGoPool(100, gopool.WithLock(new(spinlock.SpinLock)))
defer pool.Release()
for i := 0; i < 1000; i++ {
pool.AddTask(func() (interface{}, error) {
time.Sleep(10 * time.Millisecond)
return nil, nil
})
}
pool.Wait()
})
})
Describe("With Error", func() {
It("should work correctly", func() {
var errTaskError = errors.New("task error")
pool := gopool.NewGoPool(100, gopool.WithErrorCallback(func(err error) {
Expect(err).To(Equal(errTaskError))
}))
defer pool.Release()
for i := 0; i < 1000; i++ {
pool.AddTask(func() (interface{}, error) {
return nil, errTaskError
})
}
pool.Wait()
})
})
Describe("With Result", func() {
It("should work correctly", func() {
var expectedResult = "task result"
pool := gopool.NewGoPool(100, gopool.WithResultCallback(func(result interface{}) {
Expect(result).To(Equal(expectedResult))
}))
defer pool.Release()
for i := 0; i < 1000; i++ {
pool.AddTask(func() (interface{}, error) {
return expectedResult, nil
})
}
pool.Wait()
})
})
Describe("With Retry", func() {
It("should work correctly", func() {
var retryCount = int32(3)
var taskError = errors.New("task error")
var taskRunCount int32 = 0
pool := gopool.NewGoPool(100, gopool.WithRetryCount(int(retryCount)))
defer pool.Release()
pool.AddTask(func() (interface{}, error) {
atomic.AddInt32(&taskRunCount, 1)
if taskRunCount <= retryCount {
return nil, taskError
}
return nil, nil
})
pool.Wait()
Expect(atomic.LoadInt32(&taskRunCount)).To(Equal(retryCount + 1))
})
})
Describe("With Timeout", func() {
It("should work correctly", func() {
var taskRun int32
pool := gopool.NewGoPool(100, gopool.WithTimeout(100*time.Millisecond), gopool.WithErrorCallback(func(err error) {
Expect(err.Error()).To(Equal("task timed out"))
atomic.StoreInt32(&taskRun, 1)
}))
defer pool.Release()
pool.AddTask(func() (interface{}, error) {
time.Sleep(200 * time.Millisecond)
return nil, nil
})
pool.Wait()
Expect(atomic.LoadInt32(&taskRun)).To(Equal(int32(1)))
})
})
Describe("With MinWorkers", func() {
It("should work correctly", func() {
var minWorkers = 50
pool := gopool.NewGoPool(100, gopool.WithMinWorkers(minWorkers))
defer pool.Release()
Expect(pool.GetWorkerCount()).To(Equal(minWorkers))
})
})
})
這段代碼將 gopool_1_test.go
中的測試用例轉(zhuǎn)換為 Ginkgo 的格式,并使用 Gomega 斷言庫來替換原先的斷言。
這段代碼基本是 ok 的,不過運(yùn)行的時(shí)候會遇到這個(gè)錯(cuò)誤:
go test ./...
/var/folders/gv/f94q_vyx2m17d_36sf0zkm0r0000gn/T/go-build3608449998/b001/gopool.test flag redefined: ginkgo.seed
panic: /var/folders/gv/f94q_vyx2m17d_36sf0zkm0r0000gn/T/go-build3608449998/b001/gopool.test flag redefined: ginkgo.seed
goroutine 1 [running]:
flag.(*FlagSet).Var(0x14000114120, {0x1049bd108, 0x104c0d4a0}, {0x1400011af70, 0xb}, {0x10486d229, 0x2a})
/usr/local/go/src/flag/flag.go:982 +0x2a4
flag.(*FlagSet).Int64Var(...)
/usr/local/go/src/flag/flag.go:769
github.com/onsi/ginkgo/config.Flags(0x14000191ba8?, {0x10485657a?, 0x989680?}, 0x1)
/Users/danielhu/go/pkg/mod/github.com/onsi/ginkgo@v1.16.5/config/config.go:75 +0xe0
github.com/onsi/ginkgo.init.0()
/Users/danielhu/go/pkg/mod/github.com/onsi/ginkgo@v1.16.5/ginkgo_dsl.go:53 +0x38
FAIL github.com/devchat-ai/gopool 3.889s
FAIL
這時(shí)候 Google 一下,可以找到 Ginkgo issue 875。改起來也很容易,在 gopool_test.go
的開頭應(yīng)該將 . "github.com/onsi/ginkgo"
改成 . "github.com/onsi/ginkgo/v2"
所以目前 go.mod
就變成了這樣:
module github.com/devchat-ai/gopool
go 1.20
require (
github.com/daniel-hutao/spinlock v0.1.0
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
)
require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
再裝一個(gè) VS Code 的 ginkgo 插件,圓滿了:
通過 ginkgo 插件運(yùn)行一下:
也可以在命令行里自己敲 ginkgo -v
感受下更詳細(xì)的結(jié)果輸出:
$ ginkgo -v
Running Suite: Gopool Suite - /Users/danielhu/go/mycode/gopool
==============================================================
Random Seed: 1692081153
Will run 7 of 7 specs
------------------------------
Gopool With Mutex should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:17
? [0.203 seconds]
------------------------------
Gopool With SpinLock should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:31
? [0.201 seconds]
------------------------------
Gopool With Error should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:45
? [0.102 seconds]
------------------------------
Gopool With Result should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:62
? [0.102 seconds]
------------------------------
Gopool With Retry should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:79
? [0.101 seconds]
------------------------------
Gopool With Timeout should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:102
? [0.202 seconds]
------------------------------
Gopool With MinWorkers should work correctly
/Users/danielhu/go/mycode/gopool/gopool_test.go:123
? [0.001 seconds]
------------------------------
Ran 7 of 7 Specs in 0.916 seconds
SUCCESS! -- 7 Passed | 0 Failed | 0 Pending | 0 Skipped
PASS
Ginkgo ran 1 suite in 4.759009833s
Test Suite Passed
到現(xiàn)在,測試相關(guān)的文件就有了3個(gè):
gopool_benchmark_test.go
gopool_suite_test.go
gopool_test.go
到此,提個(gè) PR :Refactor tests using Ginkgo and Gomega
3. 總結(jié)
因?yàn)?GPT-4 的訓(xùn)練數(shù)據(jù)是大約2年前的,也就是最近2年這個(gè)世界發(fā)生了啥它是不知道的。所以對于一些變化大的庫,對于一些版本敏感的問題,你要對 GPT 給出的代碼保持警惕。有時(shí)候你需要的代碼對版本不敏感,那無所謂;反之,及時(shí) Google 一下。文章來源:http://www.zghlxwxcb.cn/news/detail-651005.html
總之,擅用 GPT,但別只用 GPT。偶爾還是想想你的老相好“Google Bing 和度娘”。文章來源地址http://www.zghlxwxcb.cn/news/detail-651005.html
到了這里,關(guān)于用 GPT-4 給開源項(xiàng)目 GoPool 重構(gòu)測試代碼 - 每天5分鐘玩轉(zhuǎn) GPT 編程系列(8)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!