// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and

package test

import (
	"testing"
	"time"
)

func TestMsgTrackerWaitSuccess(t *testing.T) {
	msgs := []string{"a", "b", "c"}

	msgTracker := NewMsgTracker()
	msgTracker.Add(msgs...)

	for _, msg := range msgs {
		if got, want := msgTracker.Remove(msg), true; got != want {
			t.Errorf("MsgTracker.Remove(%q) got %v, want %v", msg, got, want)
		}
	}
	for _, msg := range []string{"d", "e"} {
		if got, want := msgTracker.Remove(msg), false; got != want {
			t.Errorf("MsgTracker.Remove(%q) got %v, want %v", msg, got, want)
		}
	}
	if gotErr := msgTracker.Wait(time.Millisecond); gotErr != nil {
		t.Errorf("MsgTracker.Wait() got err: %v", gotErr)
	}
}

func TestMsgTrackerWaitTimeout(t *testing.T) {
	msgs := []string{"a", "b", "c"}

	msgTracker := NewMsgTracker()
	msgTracker.Add(msgs...)

	for _, msg := range []string{"a", "c"} {
		if got, want := msgTracker.Remove(msg), true; got != want {
			t.Errorf("MsgTracker.Remove(%q) got %v, want %v", msg, got, want)
		}
	}
	if gotErr := msgTracker.Wait(time.Millisecond); gotErr == nil {
		t.Errorf("MsgTracker.Wait() should return error")
	}
}
