3. 非同期処理のあるテスト (KVS) func TestGet(t *testing.T) { // setup db.Set(“key”, “val”) for { // wait until written time.Sleep(3 * time.Second) val, _ := db.Get(“key”) // check and test value // retry or break } }
3. 非同期処理のあるテスト (KVS) for { // do something select { case <-time.After(duration): return errors.New("timeout") case <-ticker.C: continue } } for + select パターン
6. 依存のモッキング type kvsStub struct { set func(k string, v interface{}) get func(k string) (v interface{}, ok bool) } func (s *kvsStub) Set(k string, v interface{}) { if s.set == nil { panic("set field is nil") } s.set(k, v) } // Get も同様 関数フィールドを 使ったスタブ