This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/utils/queue_test.go
Lennard Brinkhaus 31bd0cf639 api-call-system-#10 (#13)
Co-authored-by: Fenpaws <soxx@fenpa.ws>
Reviewed-on: anthrove/e621-to-graph#13
Reviewed-by: SoXX <fenpaws@noreply.localhost>
Co-authored-by: Lennard Brinkhaus <lennard.brinkhaus@noreply.localhost>
Co-committed-by: Lennard Brinkhaus <lennard.brinkhaus@noreply.localhost>
2023-07-17 08:10:13 +00:00

108 lines
2.3 KiB
Go

package utils
import (
"net/http"
"reflect"
"testing"
)
type schedulerTaskImplDummy struct {
}
func (s schedulerTaskImplDummy) BasicAuth() (string, string) {
return "", ""
}
func (s schedulerTaskImplDummy) UriPath() string {
return ""
}
func (s schedulerTaskImplDummy) SendError(_ error) {
return
}
func (s schedulerTaskImplDummy) SendStatusCode(_ int) {
return
}
func (s schedulerTaskImplDummy) SendResponse(_ *http.Response) {
return
}
func TestQueue_Pop(t *testing.T) {
type fields struct {
elements []SchedulerTask
}
tests := []struct {
name string
fields fields
want SchedulerTask
wantErr bool
}{
{
name: "Pop element of empty list",
fields: fields{},
want: nil,
wantErr: true,
},
{
name: "Pop element of a filled list with three elements",
fields: fields{elements: []SchedulerTask{
schedulerTaskImplDummy{},
schedulerTaskImplDummy{},
schedulerTaskImplDummy{},
}},
want: schedulerTaskImplDummy{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
queue := &Queue{
tasks: tt.fields.elements,
}
got, err := queue.Pop()
if (err != nil) != tt.wantErr {
t.Errorf("Pop() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Pop() got = %v, want %v", got, tt.want)
}
})
}
}
func TestQueue_Push(t *testing.T) {
t.Run("Push tasks to empty queue", func(t *testing.T) {
queue := Queue{tasks: []SchedulerTask{}, notifyChannel: make(chan bool)}
go queue.WaitForElement()
task := schedulerTaskImplDummy{}
err := queue.Push(task)
if err != nil {
t.Errorf("Push() error = %v", err)
}
if len(queue.tasks) != 1 {
t.Errorf("Push() error = queue is not one")
}
if queue.tasks[0] != task {
t.Errorf("Push() error = wrong queue task in queue")
}
})
t.Run("Push tasks to filled queue", func(t *testing.T) {
queue := Queue{tasks: []SchedulerTask{schedulerTaskImplDummy{}}, notifyChannel: make(chan bool)}
go queue.WaitForElement()
task := schedulerTaskImplDummy{}
err := queue.Push(task)
if err != nil {
t.Errorf("Push() error = %v", err)
}
if len(queue.tasks) != 2 {
t.Errorf("Push() error = queue is not two")
}
if queue.tasks[1] != task {
t.Errorf("Push() error = wrong queue task in queue")
}
})
}