github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/integration/_fixtures/serial_fixture/serial_fixture_suite_test.go (about) 1 package serial_fixture_test 2 3 import ( 4 "flag" 5 "fmt" 6 "io" 7 "net/http" 8 "sync" 9 "testing" 10 "time" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 var noSerial *bool 18 19 func init() { 20 noSerial = flag.CommandLine.Bool("no-serial", false, "set to turn off serial decoration") 21 } 22 23 var SerialDecoration = []interface{}{Serial} 24 25 func TestSerialFixture(t *testing.T) { 26 RegisterFailHandler(Fail) 27 if *noSerial { 28 SerialDecoration = []interface{}{} 29 } 30 31 RunSpecs(t, "SerialFixture Suite") 32 } 33 34 var addr string 35 36 var _ = SynchronizedBeforeSuite(func() []byte { 37 server := ghttp.NewServer() 38 lock := &sync.Mutex{} 39 count := 0 40 server.RouteToHandler("POST", "/counter", func(w http.ResponseWriter, r *http.Request) { 41 lock.Lock() 42 count += 1 43 lock.Unlock() 44 w.WriteHeader(http.StatusOK) 45 }) 46 server.RouteToHandler("GET", "/counter", func(w http.ResponseWriter, r *http.Request) { 47 lock.Lock() 48 data := []byte(fmt.Sprintf("%d", count)) 49 lock.Unlock() 50 w.Write(data) 51 }) 52 return []byte(server.HTTPTestServer.URL) 53 }, func(data []byte) { 54 addr = string(data) + "/counter" 55 }) 56 57 var postToCounter = func(g Gomega) { 58 req, err := http.Post(addr, "", nil) 59 g.Ω(err).ShouldNot(HaveOccurred()) 60 g.Ω(req.StatusCode).Should(Equal(http.StatusOK)) 61 g.Ω(req.Body.Close()).Should(Succeed()) 62 } 63 64 var getCounter = func(g Gomega) string { 65 req, err := http.Get(addr) 66 g.Ω(err).ShouldNot(HaveOccurred()) 67 content, err := io.ReadAll(req.Body) 68 g.Ω(err).ShouldNot(HaveOccurred()) 69 g.Ω(req.Body.Close()).Should(Succeed()) 70 return string(content) 71 } 72 73 var _ = SynchronizedAfterSuite(func() { 74 Consistently(postToCounter, 200*time.Millisecond, 10*time.Millisecond).Should(Succeed()) 75 }, func() { 76 initialValue := getCounter(Default) 77 Consistently(func(g Gomega) { 78 currentValue := getCounter(g) 79 g.Ω(currentValue).Should(Equal(initialValue)) 80 }, 100*time.Millisecond, 10*time.Millisecond).Should(Succeed()) 81 }) 82 83 var _ = Describe("tests", func() { 84 for i := 0; i < 10; i += 1 { 85 It("runs in parallel", func() { 86 Consistently(postToCounter, 200*time.Millisecond, 10*time.Millisecond).Should(Succeed()) 87 }) 88 } 89 90 for i := 0; i < 5; i += 1 { 91 It("runs in series", SerialDecoration, func() { 92 Ω(GinkgoParallelProcess()).Should(Equal(1)) 93 initialValue := getCounter(Default) 94 Consistently(func(g Gomega) { 95 currentValue := getCounter(g) 96 g.Ω(currentValue).Should(Equal(initialValue)) 97 }, 100*time.Millisecond, 10*time.Millisecond).Should(Succeed()) 98 }) 99 } 100 })