package logger_test

import (
	"errors"
	"fmt"
	"testing"
	"time"

	"github.com/fiskerinc/cloud-services/pkg/logger"
	"github.com/fiskerinc/cloud-services/pkg/testhelper"
)

func TestLimiter(t *testing.T) {
	err := errors.New("this is a test")
	err2 := errors.New("this is a different test")
	limiter := logger.NewLogLimiter(time.Millisecond*1, 5)

	result := limiter.Check(err.Error())
	if !result {
		t.Errorf(testhelper.TestErrorTemplate, "check 1", true, result)
	}

	result = limiter.Check(err2.Error())
	if !result {
		t.Errorf(testhelper.TestErrorTemplate, "check different error", true, result)
	}

	for i := 0; i < 6; i++ {
		result = limiter.Check(err.Error())
		if result {
			t.Errorf(testhelper.TestErrorTemplate, fmt.Sprintf("check %d", i+2), false, result)
		}
	}

	time.Sleep(time.Millisecond * 1)
	result = limiter.Check(err.Error())
	if !result {
		t.Errorf(testhelper.TestErrorTemplate, "check expired", true, result)
	}
}
