package scheduler

import (
	"testing"

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

func TestBucketProcess(t *testing.T) {
	bucket := Bucket[int]{}
	actual := []int{}

	bucket.elements[1] = []int{1, 2, 3}

	double := func(element int) {
		actual = append(actual, element*2)
	}

	bucket.Process(double)

	if len(bucket.elements[1]) != 0 {
		t.Errorf("Expected bucket to be clearsd after processing, but it wasn't")
	}

	if bucket.bucket != 1 {
		t.Errorf("Expected active bucket to be incremented, but it wasn't")
	}

	expected := []int{2, 4, 6}
	for i := 0; i < len(expected); i++ {
		if actual[i] != expected[i] {
			t.Errorf(testhelper.TestErrorTemplate, "Bucket.Process()", expected, actual)
		}
	}
}

func TestBucketLoop(t *testing.T) {
	bucket := Bucket[int]{}

	stub := func(element int) {}

	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)
	bucket.Process(stub)

	if bucket.bucket != 11 {
		t.Errorf("Expected active bucket to be 11, got %d", bucket.bucket)
	}

	bucket.Process(stub)

	if bucket.bucket != 0 {
		t.Errorf("Expected active bucket to be 0, got %d", bucket.bucket)
	}
}
