mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-06-10 19:59:35 +08:00
355 lines
6.5 KiB
Go
355 lines
6.5 KiB
Go
/*
|
|
* Copyright (c) 2014-2022, Peking University Shenzhen Graduate School
|
|
*
|
|
* This file is part of MIN-Sync.
|
|
* See AUTHORS.md for complete list of MIN-Sync authors and contributors.
|
|
*
|
|
* MIN-Sync is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU Lesser General Public License as published by the Free Software Foundation,
|
|
* either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* MIN-Sync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License along with
|
|
* MIN-Sync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
* permission notice:
|
|
|
|
* The MIT License (MIT)
|
|
|
|
* Copyright (c) 2000 Arash Partow
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*/
|
|
|
|
package utils
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestZeroSlot(t *testing.T) {
|
|
s := NewSignal()
|
|
s.Emit()
|
|
}
|
|
|
|
func TestTwoListener(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
hit2 := 0
|
|
|
|
func1 := func(...interface{}) {
|
|
hit1 += 1
|
|
}
|
|
func2 := func(...interface{}) {
|
|
hit2 += 1
|
|
}
|
|
|
|
s.Connect(func1)
|
|
s.Connect(func2)
|
|
|
|
s.Emit()
|
|
|
|
if hit1 != 1 || hit2 != 1 {
|
|
t.Fatalf("Two listener failed.")
|
|
}
|
|
}
|
|
|
|
func TestOneArgument(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
num := args[0].(int)
|
|
if num != 8106 {
|
|
panic("Test one argument failed.")
|
|
}
|
|
}
|
|
|
|
s.Connect(func1)
|
|
|
|
s.Emit(8106)
|
|
|
|
if hit1 != 1 {
|
|
t.Fatalf("Two listener failed.")
|
|
}
|
|
}
|
|
|
|
func TestTwoArguments(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
num := args[0].(int)
|
|
if num != 8106 {
|
|
panic("Test two arguments failed.")
|
|
}
|
|
num1 := args[1].(int)
|
|
if num1 != 8107 {
|
|
panic("Test two arguments failed.")
|
|
}
|
|
}
|
|
|
|
s.Connect(func1)
|
|
|
|
s.Emit(8106, 8107)
|
|
|
|
if hit1 != 1 {
|
|
t.Fatalf("Two listener failed.")
|
|
}
|
|
}
|
|
|
|
func TestManualDisconnect(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
}
|
|
|
|
conn := s.Connect(func1)
|
|
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ManualDisconnect failed.")
|
|
}
|
|
|
|
if !conn.IsConnected() {
|
|
t.Fatalf("ManualDisconnect failed.")
|
|
}
|
|
|
|
conn.Disconnect()
|
|
|
|
if conn.IsConnected() {
|
|
t.Fatalf("ManualDisconnect failed.")
|
|
}
|
|
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ManualDisconnect failed.")
|
|
}
|
|
|
|
conn.Disconnect()
|
|
}
|
|
|
|
func TestManualDisconnectDestructed(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
}
|
|
|
|
conn := s.Connect(func1)
|
|
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ManualDisconnectDestructed failed.")
|
|
}
|
|
|
|
s.Clear()
|
|
if conn.IsConnected() {
|
|
t.Fatalf("ManualDisconnectDestructed failed.")
|
|
}
|
|
s.Clear()
|
|
s.Emit()
|
|
s.Clear()
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ManualDisconnectDestructed failed.")
|
|
}
|
|
}
|
|
|
|
func TestConnectSingleSlot(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
}
|
|
|
|
conn := s.ConnectSingleShot(func1)
|
|
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ConnectSingleSlot failed.")
|
|
}
|
|
|
|
if conn.IsConnected() {
|
|
t.Fatalf("ConnectSingleSlot failed.")
|
|
}
|
|
}
|
|
|
|
func TestConnectSingleShotDisconnected(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
}
|
|
|
|
conn := s.ConnectSingleShot(func1)
|
|
|
|
if !conn.IsConnected() {
|
|
t.Fatalf("ConnectSingleShotDisconnected failed.")
|
|
}
|
|
|
|
conn.Disconnect()
|
|
|
|
if conn.IsConnected() {
|
|
t.Fatalf("ConnectSingleShotDisconnected failed.")
|
|
}
|
|
|
|
s.Emit()
|
|
if hit1 != 0 {
|
|
t.Fatalf("ConnectSingleShotDisconnected failed.")
|
|
}
|
|
}
|
|
|
|
func TestConnectSingleShot1(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
}
|
|
|
|
conn := s.ConnectSingleShot(func1)
|
|
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ConnectSingleShot1 failed.")
|
|
}
|
|
|
|
if conn.IsConnected() {
|
|
t.Fatalf("ConnectSingleShot1 failed.")
|
|
}
|
|
|
|
s.Emit()
|
|
if hit1 != 1 {
|
|
t.Fatalf("ConnectSingleShot1 failed.")
|
|
}
|
|
}
|
|
|
|
func TestConnectInHandler(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
hit2 := 0
|
|
hasHandler2 := false
|
|
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
if !hasHandler2 {
|
|
s.Connect(func(i ...interface{}) {
|
|
hit2 += 1
|
|
hasHandler2 = true
|
|
})
|
|
}
|
|
}
|
|
|
|
s.Connect(func1)
|
|
|
|
s.Emit()
|
|
if hit1 != 1 || hit2 != 0 {
|
|
t.Fatalf("ConnectInHandler failed.")
|
|
}
|
|
|
|
s.Emit()
|
|
if hit1 != 2 || hit2 != 1 {
|
|
t.Fatalf("ConnectInHandler failed.")
|
|
}
|
|
}
|
|
|
|
func TestDisconnectSelfInHandler(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
|
|
var conn *Connection
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
if !conn.IsConnected() {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
conn.Disconnect()
|
|
if conn.IsConnected() {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
if s.IsEmpty() {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
}
|
|
|
|
conn = s.Connect(func1)
|
|
s.Emit()
|
|
|
|
if hit1 != 1 {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
|
|
if conn.IsConnected() {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
|
|
if !s.IsEmpty() {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
|
|
s.Emit()
|
|
|
|
if hit1 != 1 {
|
|
t.Fatalf("DisconnectSelfInHandler failed.")
|
|
}
|
|
}
|
|
|
|
func TestThrowInHandler(t *testing.T) {
|
|
s := NewSignal()
|
|
|
|
hit1 := 0
|
|
func1 := func(args ...interface{}) {
|
|
hit1 += 1
|
|
panic("hello")
|
|
}
|
|
|
|
s.Connect(func1)
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
if hit1 != 1 {
|
|
t.Fatalf("ThrowInHandler failed.")
|
|
}
|
|
}
|
|
}()
|
|
s.Emit()
|
|
}
|