summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/internal/leafnodes/synchronized_after_suite_node.go
blob: a721d0cf7f2d9203cbf79bdfd83e291ef5ea0bdb (plain)
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
package leafnodes

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"time"

	"github.com/onsi/ginkgo/internal/failer"
	"github.com/onsi/ginkgo/types"
)

type synchronizedAfterSuiteNode struct {
	runnerA *runner
	runnerB *runner

	outcome types.SpecState
	failure types.SpecFailure
	runTime time.Duration
}

func NewSynchronizedAfterSuiteNode(bodyA interface{}, bodyB interface{}, codeLocation types.CodeLocation, timeout time.Duration, failer *failer.Failer) SuiteNode {
	return &synchronizedAfterSuiteNode{
		runnerA: newRunner(bodyA, codeLocation, timeout, failer, types.SpecComponentTypeAfterSuite, 0),
		runnerB: newRunner(bodyB, codeLocation, timeout, failer, types.SpecComponentTypeAfterSuite, 0),
	}
}

func (node *synchronizedAfterSuiteNode) Run(parallelNode int, parallelTotal int, syncHost string) bool {
	node.outcome, node.failure = node.runnerA.run()

	if parallelNode == 1 {
		if parallelTotal > 1 {
			node.waitUntilOtherNodesAreDone(syncHost)
		}

		outcome, failure := node.runnerB.run()

		if node.outcome == types.SpecStatePassed {
			node.outcome, node.failure = outcome, failure
		}
	}

	return node.outcome == types.SpecStatePassed
}

func (node *synchronizedAfterSuiteNode) Passed() bool {
	return node.outcome == types.SpecStatePassed
}

func (node *synchronizedAfterSuiteNode) Summary() *types.SetupSummary {
	return &types.SetupSummary{
		ComponentType: node.runnerA.nodeType,
		CodeLocation:  node.runnerA.codeLocation,
		State:         node.outcome,
		RunTime:       node.runTime,
		Failure:       node.failure,
	}
}

func (node *synchronizedAfterSuiteNode) waitUntilOtherNodesAreDone(syncHost string) {
	for {
		if node.canRun(syncHost) {
			return
		}

		time.Sleep(50 * time.Millisecond)
	}
}

func (node *synchronizedAfterSuiteNode) canRun(syncHost string) bool {
	resp, err := http.Get(syncHost + "/RemoteAfterSuiteData")
	if err != nil || resp.StatusCode != http.StatusOK {
		return false
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return false
	}
	resp.Body.Close()

	afterSuiteData := types.RemoteAfterSuiteData{}
	err = json.Unmarshal(body, &afterSuiteData)
	if err != nil {
		return false
	}

	return afterSuiteData.CanRun
}