<commit_before>// Copyright 2014-2015 The DevMine authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package errbag

import (
	"errors"
	"testing"
	"time"
)

func TestErrBag(t *testing.T) {
	var waitTime, errBagSize, leakInterval uint

	// test some bad input
	waitTime, errBagSize, leakInterval = 5, 60, 99
	if _, err := New(waitTime, errBagSize, leakInterval); err == nil {
		t.Fatal("leak interval < 100 shall not be permitted")
	}
	leakInterval = 1000
	errBagSize = 0
	if _, err := New(waitTime, errBagSize, leakInterval); err == nil {
		t.Fatal("errBagSize of 0 shall not be permitted")
	}
	errBagSize = 60
	waitTime = 0
	if _, err := New(waitTime, errBagSize, leakInterval); err == nil {
		t.Fatal("waitTime of 0 shall not be permitted")
	}

	waitTime = 5
	errBag, err := New(waitTime, errBagSize, leakInterval)
	if err != nil {
		t.Fatal(err)
	}

	errBag.Inflate()

	err = errors.New("foo error")
	var i uint

	// test that it does not block on less than 1 error per second
	start := time.Now()
	for i = 0; i < 4; i++ {
		errBag.Record(err)
	}
	elapsed := time.Since(start)
	if elapsed > time.Second*time.Duration(waitTime) {
		t.Fatal("throttling when error rate is low")
	}

	// now test throttling
	start = time.Now()
	for i = 0; i < errBagSize+3; i++ {
		errBag.Record(err)
	}
	elapsed = time.Since(start)
	if elapsed < time.Second*time.Duration(waitTime) {
		t.Fatal("failed to throttle")
	}

	// errBag is full of errors, deflate shall empty it
	errBag.Deflate()

	// attempting to record errors now shall panic
	defer func() {
		if r := recover(); r == nil {
			t.Fatal("call to Record() shall panic")
		}
	}()
	errBag.Record(err)
}<commit_msg>errbag/test: wait for the errbag to empty before running new test<commit_after>// Copyright 2014-2015 The DevMine authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package errbag

import (
	"errors"
	"testing"
	"time"
)

func TestErrBag(t *testing.T) {
	var waitTime, errBagSize, leakInterval uint

	// test some bad input
	waitTime, errBagSize, leakInterval = 5, 60, 99
	if _, err := New(waitTime, errBagSize, leakInterval); err == nil {
		t.Fatal("leak interval < 100 shall not be permitted")
	}
	leakInterval = 1000
	errBagSize = 0
	if _, err := New(waitTime, errBagSize, leakInterval); err == nil {
		t.Fatal("errBagSize of 0 shall not be permitted")
	}
	errBagSize = 60
	waitTime = 0
	if _, err := New(waitTime, errBagSize, leakInterval); err == nil {
		t.Fatal("waitTime of 0 shall not be permitted")
	}

	waitTime = 5
	errBag, err := New(waitTime, errBagSize, leakInterval)
	if err != nil {
		t.Fatal(err)
	}

	errBag.Inflate()

	err = errors.New("foo error")
	var i uint

	// test that it does not block on less than 1 error per second
	start := time.Now()
	for i = 0; i < 4; i++ {
		errBag.Record(err)
	}
	elapsed := time.Since(start)
	if elapsed > time.Second*time.Duration(waitTime) {
		t.Fatal("throttling when error rate is low")
	}

	// make sure the error pipeline is empty before starting new test
	time.Sleep(time.Duration(4) * time.Second)

	// now test throttling
	start = time.Now()
	for i = 0; i < errBagSize+3; i++ {
		errBag.Record(err)
	}
	elapsed = time.Since(start)
	if elapsed < time.Second*time.Duration(waitTime) {
		t.Fatal("failed to throttle")
	}

	// errBag is full of errors, deflate shall empty it
	errBag.Deflate()

	// attempting to record errors now shall panic
	defer func() {
		if r := recover(); r == nil {
			t.Fatal("call to Record() shall panic")
		}
	}()
	errBag.Record(err)
}
{'fraction_non_alphanumeric': 0.12445175438596491, 'fraction_numerical': 0.01781798245614035, 'mean_word_length': 3.544209215442092, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5861507', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1255, 'n_words': 429}