github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/test_framework/util/testcase.go (about)

     1  package util
     2  
     3  import (
     4  	"errors"
     5  	"math"
     6  	"math/rand"
     7  	"os"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  // --------------------------------------------------------------------
    13  // Test groups, system properties and other annotations modifying tests
    14  // --------------------------------------------------------------------
    15  const (
    16  	SYSPROP_NIGHTLY = "tests_nightly"
    17  )
    18  
    19  // -----------------------------------------------------------------
    20  // Truly immutable fields and constants, initialized once and valid
    21  // for all suites ever since.
    22  // -----------------------------------------------------------------
    23  
    24  // True if and only if tests are run in verbose mode. If this flag is false
    25  // tests are not expected toprint and messages.
    26  var VERBOSE = ("true" == or(os.Getenv("tests_verbose"), "false"))
    27  
    28  var INFOSTREAM = ("true" == or(os.Getenv("tests_infostream"), strconv.FormatBool(VERBOSE)))
    29  
    30  // A random multiplier which you should use when writing random tests:
    31  // multiply it by the number of iterations to scale your tests (for nightly builds).
    32  var RANDOM_MULTIPLIER = func() int {
    33  	n, err := strconv.Atoi(or(os.Getenv("tests_multiplier"), "1"))
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	return n
    38  }()
    39  
    40  var (
    41  	// Gets the codc to run tests with.
    42  	TEST_CODEC = or(os.Getenv("tests_codec"), "Lucene49") // TODO prefer random
    43  
    44  	// Gets the postingsFormat to run tests with.
    45  	TEST_POSTINGSFORMAT = or(os.Getenv("tests_postingsformat"), "random")
    46  
    47  	// Gets the docValuesFormat to run tests with
    48  	TEST_DOCVALUESFORMAT = or(os.Getenv("tests_docvaluesformat"), "random")
    49  
    50  	// Gets the directory to run tests with
    51  	TEST_DIRECTORY = or(os.Getenv("tests_directory"), "random")
    52  )
    53  
    54  // Whether or not Nightly tests should run
    55  var TEST_NIGHTLY = ("true" == or(os.Getenv(SYSPROP_NIGHTLY), "false"))
    56  
    57  func or(a, b string) string {
    58  	if len(a) > 0 {
    59  		return a
    60  	}
    61  	return b
    62  }
    63  
    64  // L332
    65  
    66  // -----------------------------------------------------------------
    67  // Fields initialized in class or instance rules.
    68  // -----------------------------------------------------------------
    69  
    70  var PREFLEX_IMPERSONATION_IS_ACTIVE bool
    71  
    72  /*
    73  When true, Codecs fo rold Lucene version will support writing indexes
    74  in that format. Defaults to false, can be disabled by specific tests
    75  on demand.
    76  */
    77  var OLD_FORMAT_IMPERSONATION_IS_ACTIVE = false
    78  
    79  // -----------------------------------------------------------------
    80  // Class level (suite) rules.
    81  // -----------------------------------------------------------------
    82  
    83  // Suite failure marker (any error in the test or suite scope)
    84  var SuiteFailureMarker = &TestRuleMarkFailure{}
    85  
    86  // -----------------------------------------------------------------
    87  // Test facilities and facades for subclasses.
    88  // -----------------------------------------------------------------
    89  
    90  /*
    91  Note it's different from Lucene's Randomized Test Runner.
    92  
    93  There is an overhead connected with getting the Random for a particular
    94  context and thread. It is better to cache this Random locally if tight loops
    95  with multiple invocations are present or create a derivative local Random for
    96  millions of calls like this:
    97  
    98  		r := rand.New(rand.NewSource(99))
    99  		// tight loop with many invocations.
   100  
   101  */
   102  func Random() *rand.Rand {
   103  	return rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
   104  }
   105  
   106  // L643
   107  
   108  /*
   109  Returns a number of at least i
   110  
   111  The actual number returned will be influenced by whether TEST_NIGHTLY
   112  is active and RANDOM_MULTIPLIER, but also with some random fudge.
   113  */
   114  func atLeastBy(random *rand.Rand, i int) int {
   115  	min := i * RANDOM_MULTIPLIER
   116  	if TEST_NIGHTLY {
   117  		min = 2 * min
   118  	}
   119  	max := min + min/2
   120  	return NextInt(random, min, max)
   121  }
   122  
   123  func AtLeast(i int) int {
   124  	return atLeastBy(Random(), i)
   125  }
   126  
   127  /*
   128  Returns true if something should happen rarely,
   129  
   130  The actual number returned will be influenced by whether TEST_NIGHTLY
   131  is active and RANDOM_MULTIPLIER
   132  */
   133  func Rarely(random *rand.Rand) bool {
   134  	p := either(TEST_NIGHTLY, 10, 1).(int)
   135  	p += int(float64(p) * math.Log(float64(RANDOM_MULTIPLIER)))
   136  	if p > 50 {
   137  		p = 50
   138  	}
   139  	min := 100 - p // never more than 50
   140  	return random.Intn(100) >= min
   141  }
   142  
   143  func Usually(r *rand.Rand) bool {
   144  	return !Rarely(r)
   145  }
   146  
   147  func either(flag bool, value, orValue interface{}) interface{} {
   148  	if flag {
   149  		return value
   150  	}
   151  	return orValue
   152  }
   153  
   154  /*
   155  Assumption is different from Assert that Assumption returns error,
   156  while Assert panics.
   157  */
   158  func AssumeTrue(msg string, ok bool) error {
   159  	if !ok {
   160  		return errors.New(msg)
   161  	}
   162  	return nil
   163  }
   164  
   165  func AssumeFalse(msg string, ok bool) error {
   166  	if !ok {
   167  		return errors.New(msg)
   168  	}
   169  	return nil
   170  }