<commit_before>package supervisor

import (
	"context"
	"fmt"
	"sync/atomic"
	"testing"
	"time"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
)

func TestIntensityError(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		return errors.Errorf("DUMMY")
	},
		Intensity(3),
		Period(time.Millisecond*50))
	assert.Equal(t, int64(3), sum)
}

func TestIntensityPanic(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		panic(errors.Errorf("DUMMY"))
	},
		Intensity(3),
		Period(time.Millisecond*50))
	assert.Equal(t, int64(3), sum)
}

func TestIntensityNoError(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		return nil
	},
		Intensity(3),
		Period(time.Millisecond*50))
	assert.Equal(t, int64(1), sum)
}

func TestOnError(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		panic("X")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(error) { atomic.AddInt64(&sum, 1) }))
	assert.Equal(t, int64(6), sum)
}

func TestOnError2(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		panic("X")
	},
		Intensity(3000),
		Period(time.Microsecond),
		OnError(func(error) { atomic.AddInt64(&sum, 1) }))
	assert.Equal(t, int64(6000), sum)
}

func TestOnError3(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	var sum int64
	go Supervise(func() error {
		select {
		case <-ctx.Done():
			return nil
		default:
		}
		atomic.AddInt64(&sum, 1)
		panic("X")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(error) { cancel() }))
	<-ctx.Done()
	assert.Equal(t, int64(1), sum)
}

func ExampleSupervise() {
	// if should run concurrently then use "go Supervise(...)"
	Supervise(func() error {
		fmt.Println("done")
		return nil
	})

	// Output:
	// done
}

func ExampleIntensity() {
	Supervise(func() error {
		fmt.Println(1)
		return errors.Errorf("FAILED")
	},
		Intensity(3),
		Period(time.Millisecond*50))

	// Output:
	// 1
	// 1
	// 1
}

func ExampleOnError() {
	Supervise(func() error {
		return errors.Errorf("FAILED")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(err error) { fmt.Println(err) }))

	// Output:
	// FAILED
	// FAILED
	// FAILED
}<commit_msg>more examples added<commit_after>package supervisor

import (
	"context"
	"fmt"
	"sync/atomic"
	"testing"
	"time"

	"github.com/pkg/errors"
	"github.com/stretchr/testify/assert"
)

func TestIntensityError(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		return errors.Errorf("DUMMY")
	},
		Intensity(3),
		Period(time.Millisecond*50))
	assert.Equal(t, int64(3), sum)
}

func TestIntensityPanic(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		panic(errors.Errorf("DUMMY"))
	},
		Intensity(3),
		Period(time.Millisecond*50))
	assert.Equal(t, int64(3), sum)
}

func TestIntensityNoError(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		return nil
	},
		Intensity(3),
		Period(time.Millisecond*50))
	assert.Equal(t, int64(1), sum)
}

func TestOnError(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		panic("X")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(error) { atomic.AddInt64(&sum, 1) }))
	assert.Equal(t, int64(6), sum)
}

func TestOnError2(t *testing.T) {
	var sum int64
	Supervise(func() error {
		atomic.AddInt64(&sum, 1)
		panic("X")
	},
		Intensity(3000),
		Period(time.Microsecond),
		OnError(func(error) { atomic.AddInt64(&sum, 1) }))
	assert.Equal(t, int64(6000), sum)
}

func TestOnError3(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	var sum int64
	go Supervise(func() error {
		select {
		case <-ctx.Done():
			return nil
		default:
		}
		atomic.AddInt64(&sum, 1)
		panic("X")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(error) { cancel() }))
	<-ctx.Done()
	assert.Equal(t, int64(1), sum)
}

func ExampleSupervise() {
	// if should run concurrently then use "go Supervise(...)"
	Supervise(func() error {
		fmt.Println("done")
		return nil
	})

	// Output:
	// done
}

func ExampleIntensity() {
	Supervise(func() error {
		fmt.Println(1)
		return errors.Errorf("FAILED")
	},
		Intensity(3),
		Period(time.Millisecond*50))

	// Output:
	// 1
	// 1
	// 1
}

func ExampleOnError_error() {
	Supervise(func() error {
		return errors.Errorf("FAILED")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(err error) { fmt.Println(err) }))

	// Output:
	// FAILED
	// FAILED
	// FAILED
}

func ExampleOnError_panic() {
	Supervise(func() error {
		panic(errors.Errorf("FAILED"))
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(err error) { fmt.Println(err) }))

	// Output:
	// FAILED
	// FAILED
	// FAILED
}

func ExamplePeriod() {
	startedAt := time.Now()
	Supervise(func() error {
		return errors.Errorf("FAILED")
	},
		Intensity(3),
		Period(time.Millisecond*50),
		OnError(func(err error) { fmt.Println(time.Since(startedAt).Round(time.Millisecond)) }))

	// Output:
	// 0s
	// 50ms
	// 100ms
}
{'fraction_non_alphanumeric': 0.17541142303969023, 'fraction_numerical': 0.036398838334946754, 'mean_word_length': 4.054794520547945, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2597149', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2078, 'n_words': 377}