package rep import ( "math/rand" "net/http" "net/http/httptest" "path/filepath" "reflect" "strings" "testing" "time" ) func TestAppHarnessRun(t *testing.T) { TestAppHarness{}.Run(t) } type TestAppHarness struct { } func (h TestAppHarness) Run(t *testing.T) { val := reflect.ValueOf(h) typ := val.Type() for i := 0; i < typ.NumMethod(); i++ { method := typ.Method(i) if !strings.HasPrefix(method.Name, "Test") { continue } t.Run(method.Name, func(t *testing.T) { //t.Parallel() rootDir := t.TempDir() app1 := newApp(t, rand.Int63(), Config{ Primary: true, RootDir: filepath.Join(rootDir, "app1"), ReplicationPSK: "123", WALSegMinCount: 1, WALSegMaxAgeSec: 1, WALSegGCAgeSec: 1, }) defer app1.Close() mux := http.NewServeMux() mux.HandleFunc("/rep/", app1.rep.Handle) testServer := httptest.NewServer(mux) defer testServer.Close() app2 := newApp(t, rand.Int63(), Config{ Primary: false, RootDir: filepath.Join(rootDir, "app2"), ReplicationPSK: "123", PrimaryEndpoint: testServer.URL + "/rep/", WALSegMinCount: 1, WALSegMaxAgeSec: 1, WALSegGCAgeSec: 1, }) val.MethodByName(method.Name).Call([]reflect.Value{ reflect.ValueOf(t), reflect.ValueOf(app1), reflect.ValueOf(app2), }) }) } } func (TestAppHarness) TestRandomUpdates(t *testing.T, app1, app2 *TestApp) { go app1.UpdateRandomFor(4 * time.Second) app2.WaitForEOF() app1.AssertEqual(t, app2) } /* func (TestAppHarness) TestRandomUpdatesReplay(t *testing.T, app1, app2 *TestApp) { app1.UpdateRandomFor(4 * time.Second) app2.WaitForEOF() app1.Close() app1 = newApp(t, app1.ID, app1.rep.conf) app1.AssertEqual(t, app2) info := app1.rep.Info() if info.AppSeqNum != 0 { t.Fatal(info) } } func (TestAppHarness) TestRandomUpdatesAck(t *testing.T, app1, app2 *TestApp) { go app1.UpdateRandomFor(4 * time.Second) app2.WaitForEOF() app1.AssertEqual(t, app2) info := app1.rep.Info() if info.AppSeqNum == 0 || info.AppSeqNum != info.WALLastSeqNum { t.Fatal(info) } } func (TestAppHarness) TestWriteThenOpenFollower(t *testing.T, app1, app2 *TestApp) { app2.Close() app1.UpdateRandomFor(4 * time.Second) app2 = newApp(t, app2.ID, app2.rep.conf) app2.WaitForEOF() app1.AssertEqual(t, app2) } func (TestAppHarness) TestUpdateOpenFollowerConcurrently(t *testing.T, app1, app2 *TestApp) { app2.Close() go app1.UpdateRandomFor(4 * time.Second) time.Sleep(2 * time.Second) app2 = newApp(t, app2.ID, app2.rep.conf) app2.WaitForEOF() app1.AssertEqual(t, app2) } func (TestAppHarness) TestUpdateCloseOpenFollowerConcurrently(t *testing.T, app1, app2 *TestApp) { go app1.UpdateRandomFor(4 * time.Second) time.Sleep(time.Second) app2.Close() time.Sleep(time.Second) app2 = newApp(t, app2.ID, app2.rep.conf) app2.WaitForEOF() app1.AssertEqual(t, app2) } */