<commit_before>package gcnotifier

import (
	"io/ioutil"
	"runtime"
	"testing"
	"time"
)

func TestAfterGC(t *testing.T) {
	doneCh := make(chan struct{})

	go func() {
		M := &runtime.MemStats{}
		NumGC := uint32(0)
		gcn := New()
		for range gcn.AfterGC() {
			runtime.ReadMemStats(M)
			NumGC += 1
			if NumGC != M.NumGC {
				t.Fatal("Skipped a GC notification")
			}
			if NumGC >= 500 {
				gcn.Close()
				gcn.Close() // harmless, just for testing repeated Close()
			}
		}
		doneCh <- struct{}{}
	}()

	for {
		select {
		case <-time.After(1 * time.Millisecond):
			b := make([]byte, 1<<20)
			b[0] = 1
		case <-doneCh:
			return
		}
	}
}

func TestAutoclose(t *testing.T) {
	count := 10000
	done := make(chan struct{})
	go func() {
		for i := 0; i < count; i++ {
			gcn := New().AfterGC()
			go func() {
				for range gcn {
				}
				// to reach here autoclose() must have been called
				done <- struct{}{}
			}()
		}
	}()
	for i := 0; i < count; {
		select {
		case <-done:
			i++
		default:
			runtime.GC() // required to quickly trigger autoclose()
		}
	}
}<commit_msg>split double close test to a new test<commit_after>package gcnotifier

import (
	"io/ioutil"
	"runtime"
	"testing"
	"time"
)

func TestAfterGC(t *testing.T) {
	doneCh := make(chan struct{})

	go func() {
		M := &runtime.MemStats{}
		NumGC := uint32(0)
		gcn := New()
		for range gcn.AfterGC() {
			runtime.ReadMemStats(M)
			NumGC += 1
			if NumGC != M.NumGC {
				t.Fatal("Skipped a GC notification")
			}
			if NumGC >= 500 {
				gcn.Close()
			}
		}
		doneCh <- struct{}{}
	}()

	for {
		select {
		case <-time.After(1 * time.Millisecond):
			b := make([]byte, 1<<20)
			b[0] = 1
		case <-doneCh:
			return
		}
	}
}

func TestDoubleClose(t *testing.T) {
	gcn := New()
	gcn.Close()
	gcn.Close() // no-op
}

func TestAutoclose(t *testing.T) {
	count := 10000
	done := make(chan struct{})
	go func() {
		for i := 0; i < count; i++ {
			gcn := New().AfterGC()
			go func() {
				for range gcn {
				}
				// to reach here autoclose() must have been called
				done <- struct{}{}
			}()
		}
	}()
	for i := 0; i < count; {
		select {
		case <-done:
			i++
		default:
			runtime.GC() // required to quickly trigger autoclose()
		}
	}
}
{'fraction_non_alphanumeric': 0.1767631103074141, 'fraction_numerical': 0.018083182640144666, 'mean_word_length': 2.518282988871224, 'pattern_counts': {'":': 0, '<': 21, '<?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': '3260810', 'n_tokens_mistral': 1059, 'n_tokens_neox': 879, 'n_words': 226}