Exemplo n.º 1
0
func TestOffset(t *testing.T) {
	for _, expectedOffset := range []time.Duration{-time.Hour, time.Hour} {
		if err := os.Setenv("COCKROACH_SIMULATED_OFFSET", expectedOffset.String()); err != nil {
			t.Fatal(err)
		}

		envutil.ClearEnvCache()

		initFakeTime()

		lowerBound := time.Now().Add(expectedOffset)
		offsetTime := Now()
		upperBound := time.Now().Add(expectedOffset)

		if offsetTime.Before(lowerBound) || offsetTime.After(upperBound) {
			t.Errorf("expected offset time %s to be in the interval\n[%s,%s]", offsetTime, lowerBound, upperBound)
		}
	}
}
Exemplo n.º 2
0
// TestReadEnvironmentVariables verifies that all environment variables are
// correctly parsed.
func TestReadEnvironmentVariables(t *testing.T) {
	defer leaktest.AfterTest(t)()

	resetEnvVar := func() {
		// Reset all environment variables in case any were already set.
		if err := os.Unsetenv("COCKROACH_LINEARIZABLE"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_MAX_OFFSET"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_METRICS_SAMPLE_INTERVAL"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_SCAN_INTERVAL"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_SCAN_MAX_IDLE_TIME"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_CONSISTENCY_CHECK_INTERVAL"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_CONSISTENCY_CHECK_PANIC_ON_FAILURE"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_TIME_UNTIL_STORE_DEAD"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_CONSISTENCY_CHECK_INTERVAL"); err != nil {
			t.Fatal(err)
		}
		if err := os.Unsetenv("COCKROACH_RESERVATIONS_ENABLED"); err != nil {
			t.Fatal(err)
		}
	}
	defer resetEnvVar()

	// Makes sure no values are set when no environment variables are set.
	ctx := MakeContext()
	ctxExpected := MakeContext()

	resetEnvVar()
	ctx.readEnvironmentVariables()
	if !reflect.DeepEqual(ctx, ctxExpected) {
		t.Fatalf("actual context does not match expected:\nactual:%+v\nexpected:%+v", ctx, ctxExpected)
	}

	// Set all the environment variables to valid values and ensure they are set
	// correctly.
	if err := os.Setenv("COCKROACH_LINEARIZABLE", "true"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.Linearizable = true
	if err := os.Setenv("COCKROACH_MAX_OFFSET", "1s"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.MaxOffset = time.Second
	if err := os.Setenv("COCKROACH_METRICS_SAMPLE_INTERVAL", "1h10m"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.MetricsSampleInterval = time.Hour + time.Minute*10
	if err := os.Setenv("COCKROACH_SCAN_INTERVAL", "48h"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.ScanInterval = time.Hour * 48
	if err := os.Setenv("COCKROACH_SCAN_MAX_IDLE_TIME", "100ns"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.ScanMaxIdleTime = time.Nanosecond * 100
	if err := os.Setenv("COCKROACH_CONSISTENCY_CHECK_INTERVAL", "48h"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.ConsistencyCheckInterval = time.Hour * 48
	if err := os.Setenv("COCKROACH_CONSISTENCY_CHECK_PANIC_ON_FAILURE", "true"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.ConsistencyCheckPanicOnFailure = true
	if err := os.Setenv("COCKROACH_TIME_UNTIL_STORE_DEAD", "10ms"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.TimeUntilStoreDead = time.Millisecond * 10
	if err := os.Setenv("COCKROACH_CONSISTENCY_CHECK_INTERVAL", "10ms"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.ConsistencyCheckInterval = time.Millisecond * 10
	if err := os.Setenv("COCKROACH_RESERVATIONS_ENABLED", "false"); err != nil {
		t.Fatal(err)
	}
	ctxExpected.ReservationsEnabled = false

	envutil.ClearEnvCache()
	ctx.readEnvironmentVariables()
	if !reflect.DeepEqual(ctx, ctxExpected) {
		t.Fatalf("actual context does not match expected:\nactual:%+v\nexpected:%+v", ctx, ctxExpected)
	}

	// Set all the environment variables to invalid values and test that the
	// defaults are still set.
	ctx = MakeContext()
	ctxExpected = MakeContext()

	if err := os.Setenv("COCKROACH_LINEARIZABLE", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_MAX_OFFSET", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_METRICS_SAMPLE_INTERVAL", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_SCAN_INTERVAL", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_SCAN_MAX_IDLE_TIME", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_CONSISTENCY_CHECK_INTERVAL", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_CONSISTENCY_CHECK_PANIC_ON_FAILURE", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_TIME_UNTIL_STORE_DEAD", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_CONSISTENCY_CHECK_INTERVAL", "abcd"); err != nil {
		t.Fatal(err)
	}
	if err := os.Setenv("COCKROACH_RESERVATIONS_ENABLED", "abcd"); err != nil {
		t.Fatal(err)
	}

	envutil.ClearEnvCache()
	ctx.readEnvironmentVariables()
	if !reflect.DeepEqual(ctx, ctxExpected) {
		t.Fatalf("actual context does not match expected:\nactual:%+v\nexpected:%+v", ctx, ctxExpected)
	}
}