/*
 * Copyright 2021 CloudWeGo Authors
 *
 * 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
 *
 *     http://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
 * limitations under the License.
 */

package event

import (
	"sync"
	"testing"
	"time"

	"github.com/cloudwego/kitex/internal/test"
)

func TestWatch(t *testing.T) {
	ebus := NewEventBus()

	var triggered bool
	var mu sync.RWMutex
	ebus.Watch("trigger", func(event *Event) {
		mu.Lock()
		triggered = true
		mu.Unlock()
	})
	mu.RLock()
	test.Assert(t, !triggered)
	mu.RUnlock()

	ebus.Dispatch(&Event{Name: "not-trigger"})
	time.Sleep(time.Millisecond)
	mu.RLock()
	test.Assert(t, !triggered)
	mu.RUnlock()

	ebus.Dispatch(&Event{Name: "trigger"})
	time.Sleep(time.Millisecond)
	mu.RLock()
	test.Assert(t, triggered)
	mu.RUnlock()
}

func TestUnWatch(t *testing.T) {
	ebus := NewEventBus()

	var triggered bool
	var notTriggered bool
	var mu sync.RWMutex
	triggeredHandler := func(event *Event) {
		mu.Lock()
		triggered = true
		mu.Unlock()
	}
	notTriggeredHandler := func(event *Event) {
		mu.Lock()
		notTriggered = true
		mu.Unlock()
	}

	ebus.Watch("trigger", triggeredHandler)
	ebus.Watch("trigger", notTriggeredHandler)
	ebus.Unwatch("trigger", notTriggeredHandler)

	ebus.Dispatch(&Event{Name: "trigger"})
	time.Sleep(time.Millisecond)
	mu.RLock()
	test.Assert(t, triggered)
	test.Assert(t, !notTriggered)
	mu.RUnlock()
}

func TestDispatchAndWait(t *testing.T) {
	ebus := NewEventBus()

	var triggered bool
	var mu sync.RWMutex
	delayHandler := func(event *Event) {
		time.Sleep(time.Second)
		mu.Lock()
		triggered = true
		mu.Unlock()
	}

	ebus.Watch("trigger", delayHandler)

	ebus.DispatchAndWait(&Event{Name: "trigger"})
	mu.RLock()
	test.Assert(t, triggered)
	mu.RUnlock()
}
