summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pborman/uuid/seq_test.go
blob: 3b3d1430d513f9021ab1923d1a4d50e4efcbc57a (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
// Copyright 2014 Google Inc.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package uuid

import (
	"flag"
	"runtime"
	"testing"
	"time"
)

// This test is only run when --regressions is passed on the go test line.
var regressions = flag.Bool("regressions", false, "run uuid regression tests")

// TestClockSeqRace tests for a particular race condition of returning two
// identical Version1 UUIDs.  The duration of 1 minute was chosen as the race
// condition, before being fixed, nearly always occured in under 30 seconds.
func TestClockSeqRace(t *testing.T) {
	if !*regressions {
		t.Skip("skipping regression tests")
	}
	duration := time.Minute

	done := make(chan struct{})
	defer close(done)

	ch := make(chan UUID, 10000)
	ncpu := runtime.NumCPU()
	switch ncpu {
	case 0, 1:
		// We can't run the test effectively.
		t.Skip("skipping race test, only one CPU detected")
		return
	default:
		runtime.GOMAXPROCS(ncpu)
	}
	for i := 0; i < ncpu; i++ {
		go func() {
			for {
				select {
				case <-done:
					return
				case ch <- NewUUID():
				}
			}
		}()
	}

	uuids := make(map[string]bool)
	cnt := 0
	start := time.Now()
	for u := range ch {
		s := u.String()
		if uuids[s] {
			t.Errorf("duplicate uuid after %d in %v: %s", cnt, time.Since(start), s)
			return
		}
		uuids[s] = true
		if time.Since(start) > duration {
			return
		}
		cnt++
	}
}