Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | // Package testutil provides initalization and utility routines for unit tests. |
| 2 | // |
| 3 | // All tests should import it, even if only for its initialization: |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 4 | // import _ "veyron.io/veyron/veyron/lib/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | // |
| 6 | package testutil |
| 7 | |
| 8 | import ( |
| 9 | "flag" |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 10 | "math/rand" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 11 | "os" |
| 12 | "runtime" |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 13 | "strconv" |
Asim Shankar | 2028203 | 2014-07-07 22:48:00 -0700 | [diff] [blame] | 14 | "sync" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 15 | // Need to import all of the packages that could possibly |
| 16 | // define flags that we care about. In practice, this is the |
| 17 | // flags defined by the testing package, the logging library |
| 18 | // and any flags defined by the blackbox package below. |
| 19 | _ "testing" |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 20 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 21 | |
| 22 | // Import blackbox to ensure that it gets to define its flags. |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 23 | _ "veyron.io/veyron/veyron/lib/testutil/blackbox" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 24 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 25 | "veyron.io/veyron/veyron2/vlog" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 28 | const ( |
| 29 | SeedEnv = "VEYRON_RNG_SEED" |
| 30 | ) |
| 31 | |
Asim Shankar | 2028203 | 2014-07-07 22:48:00 -0700 | [diff] [blame] | 32 | // Random is a concurrent-access friendly source of randomness. |
| 33 | type Random struct { |
| 34 | mu sync.Mutex |
| 35 | rand *rand.Rand |
| 36 | } |
| 37 | |
| 38 | // Int returns a non-negative pseudo-random int. |
| 39 | func (r *Random) Int() int { |
| 40 | r.mu.Lock() |
| 41 | defer r.mu.Unlock() |
| 42 | return r.rand.Int() |
| 43 | } |
| 44 | |
| 45 | // Intn returns a non-negative pseudo-random int in the range [0, n). |
| 46 | func (r *Random) Intn(n int) int { |
| 47 | r.mu.Lock() |
| 48 | defer r.mu.Unlock() |
| 49 | return r.rand.Intn(n) |
| 50 | } |
| 51 | |
| 52 | // Int63 returns a non-negative 63-bit pseudo-random integer as an int64. |
| 53 | func (r *Random) Int63() int64 { |
| 54 | r.mu.Lock() |
| 55 | defer r.mu.Unlock() |
| 56 | return r.rand.Int63() |
| 57 | } |
| 58 | |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 59 | var ( |
Asim Shankar | 2028203 | 2014-07-07 22:48:00 -0700 | [diff] [blame] | 60 | Rand *Random |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 61 | ) |
| 62 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 63 | func init() { |
| 64 | if os.Getenv("GOMAXPROCS") == "" { |
| 65 | // Set the number of logical processors to the number of CPUs, |
| 66 | // if GOMAXPROCS is not set in the environment. |
| 67 | runtime.GOMAXPROCS(runtime.NumCPU()) |
| 68 | } |
Jiri Simsa | 01cf937 | 2014-06-30 09:49:55 -0700 | [diff] [blame] | 69 | // At this point all of the flags that we're going to use for |
| 70 | // tests must be defined. |
| 71 | flag.Parse() |
| 72 | vlog.ConfigureLibraryLoggerFromFlags() |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 73 | // Initialize pseudo-random number generator. |
| 74 | seed := time.Now().UnixNano() |
| 75 | seedString := os.Getenv(SeedEnv) |
| 76 | if seedString != "" { |
| 77 | var err error |
| 78 | base, bitSize := 0, 64 |
Jiri Simsa | f659851 | 2014-06-30 10:32:02 -0700 | [diff] [blame] | 79 | seed, err = strconv.ParseInt(seedString, base, bitSize) |
Jiri Simsa | 870ddd6 | 2014-06-27 14:56:58 -0700 | [diff] [blame] | 80 | if err != nil { |
| 81 | vlog.Fatalf("ParseInt(%v, %v, %v) failed: %v", seedString, base, bitSize, err) |
| 82 | } |
| 83 | } |
| 84 | vlog.Infof("Seeding pseudo-random number generator with %v", seed) |
Asim Shankar | 2028203 | 2014-07-07 22:48:00 -0700 | [diff] [blame] | 85 | Rand = &Random{rand: rand.New(rand.NewSource(seed))} |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 86 | } |