<commit_before>package retry

import (
	"errors"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestRetry(t *testing.T) {
	t.Log("it does not retryies if no error")
	{
		retryCnt := 0

		err := Times(2).Try(func(attempt uint) error {
			retryCnt++
			return nil
		})

		require.NoError(t, err)
		require.Equal(t, 1, retryCnt)
	}

	t.Log("it does retry if error")
	{
		attemptCnt := 0
		err := Times(2).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 3, attemptCnt)
	}

	t.Log("it does not retry if Times=0")
	{
		attemptCnt := 0

		err := Times(0).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 1, attemptCnt)
	}

	t.Log("it does a total attempt of 2 if Times=1")
	{
		attemptCnt := 0

		err := Times(1).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 2, attemptCnt)
	}

	t.Log("it does a total attempt of 5 if Times=4")
	{
		attemptCnt := 0

		err := Times(4).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 5, attemptCnt)
	}

	t.Log("it does not wait before first execution")
	{
		attemptCnt := 0
		startTime := time.Now()

		err := Times(1).Wait(3).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		duration := time.Now().Sub(startTime)

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 2, attemptCnt)
		if duration >= time.Duration(4)*time.Second {
			t.Fatalf("Should take no more than 10 sec, but got: %s", duration)
		}
	}

	t.Log("it waits before second execution")
	{
		attemptCnt := 0
		startTime := time.Now()

		err := Times(1).Wait(4).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		duration := time.Now().Sub(startTime)

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 2, attemptCnt)
		if duration < time.Duration(3)*time.Second {
			t.Fatalf("Should take at least 10 sec, but got: %s", duration)
		}
	}
}

func TestWait(t *testing.T) {
	t.Log("it creates retry model with wait time")
	{
		helper := Wait(3)
		require.Equal(t, uint(3), helper.waitSec)
	}

	t.Log("it creates retry model with wait time")
	{
		helper := Wait(3)
		helper.Wait(5)
		require.Equal(t, uint(5), helper.waitSec)
	}
}

func TestTimes(t *testing.T) {
	t.Log("it creates retry model with retry times")
	{
		helper := Times(3)
		require.Equal(t, uint(3), helper.retry)
	}

	t.Log("it sets retry times")
	{
		helper := Times(3)<commit_msg>retry test - just log text fix<commit_after>package retry

import (
	"errors"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestRetry(t *testing.T) {
	t.Log("it does not retryies if no error")
	{
		retryCnt := 0

		err := Times(2).Try(func(attempt uint) error {
			retryCnt++
			return nil
		})

		require.NoError(t, err)
		require.Equal(t, 1, retryCnt)
	}

	t.Log("it does retry if error")
	{
		attemptCnt := 0
		err := Times(2).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 3, attemptCnt)
	}

	t.Log("it does not retry if Times=0")
	{
		attemptCnt := 0

		err := Times(0).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 1, attemptCnt)
	}

	t.Log("it does a total attempt of 2 if Times=1")
	{
		attemptCnt := 0

		err := Times(1).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 2, attemptCnt)
	}

	t.Log("it does a total attempt of 5 if Times=4")
	{
		attemptCnt := 0

		err := Times(4).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 5, attemptCnt)
	}

	t.Log("it does not wait before first execution")
	{
		attemptCnt := 0
		startTime := time.Now()

		err := Times(1).Wait(3).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		duration := time.Now().Sub(startTime)

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 2, attemptCnt)
		if duration >= time.Duration(4)*time.Second {
			t.Fatalf("Should take no more than 4 sec, but got: %s", duration)
		}
	}

	t.Log("it waits before second execution")
	{
		attemptCnt := 0
		startTime := time.Now()

		err := Times(1).Wait(4).Try(func(attempt uint) error {
			attemptCnt++
			return errors.New("error")
		})

		duration := time.Now().Sub(startTime)

		require.Error(t, err)
		require.Equal(t, "error", err.Error())
		require.Equal(t, 2, attemptCnt)
		if duration < time.Duration(3)*time.Second {
			t.Fatalf("Should take at least 3 sec, but got: %s", duration)
		}
	}
}

func TestWait(t *testing.T) {
	t.Log("it creates retry model with wait time")
	{
		helper := Wait(3)
		require.Equal(t, uint(3), helper.waitSec)
	}

	t.Log("it creates retry model with wait time")
	{
		helper := Wait(3)
		helper.Wait(5)
		require.Equal(t, uint(5), helper.waitSec)
	}
}

func TestTimes(t *testing.T) {
	t.Log("it creates retry model with retry times")
	{
		helper := Times(3)
		require.Equal(t, uint(3), helper.retry)
	}

	t.Log("it sets retry times")
	{
		helper := Times(3)
{'fraction_non_alphanumeric': 0.1676823959602995, 'fraction_numerical': 0.014278251784781473, 'mean_word_length': 3.5228346456692914, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3471059', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2176, 'n_words': 520}