summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/internal/spec_iterator/sharded_parallel_spec_iterator.go
blob: ad4a3ea3c6473bf8192e1068fe5b3c5a492abc57 (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
package spec_iterator

import "github.com/onsi/ginkgo/internal/spec"

type ShardedParallelIterator struct {
	specs    []*spec.Spec
	index    int
	maxIndex int
}

func NewShardedParallelIterator(specs []*spec.Spec, total int, node int) *ShardedParallelIterator {
	startIndex, count := ParallelizedIndexRange(len(specs), total, node)

	return &ShardedParallelIterator{
		specs:    specs,
		index:    startIndex,
		maxIndex: startIndex + count,
	}
}

func (s *ShardedParallelIterator) Next() (*spec.Spec, error) {
	if s.index >= s.maxIndex {
		return nil, ErrClosed
	}

	spec := s.specs[s.index]
	s.index += 1
	return spec, nil
}

func (s *ShardedParallelIterator) NumberOfSpecsPriorToIteration() int {
	return len(s.specs)
}

func (s *ShardedParallelIterator) NumberOfSpecsToProcessIfKnown() (int, bool) {
	return s.maxIndex - s.index, true
}

func (s *ShardedParallelIterator) NumberOfSpecsThatWillBeRunIfKnown() (int, bool) {
	count := 0
	for i := s.index; i < s.maxIndex; i += 1 {
		if !s.specs[i].Skipped() && !s.specs[i].Pending() {
			count += 1
		}
	}
	return count, true
}