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