2023-12-23 14:33:52 +08:00
|
|
|
package observer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2024-11-15 23:34:35 +08:00
|
|
|
"time"
|
2023-12-23 14:33:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestObserver(t *testing.T) {
|
|
|
|
sendEmail := make(Observer)
|
|
|
|
notifyWelcome := make(Observer)
|
|
|
|
|
|
|
|
userRegister := NewSubject()
|
|
|
|
userRegister.Attach(sendEmail, notifyWelcome)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for data := range sendEmail {
|
|
|
|
fmt.Println("The send email service receive data: ", data)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for data := range notifyWelcome {
|
|
|
|
fmt.Println("The notify welcome service receive data: ", data)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
newUser1 := "fantasticbin"
|
|
|
|
newUser2 := "gan"
|
|
|
|
userRegister.Notify(newUser1)
|
|
|
|
userRegister.Notify(newUser2)
|
2024-11-16 14:10:32 +08:00
|
|
|
// 休眠防止主协程退出
|
2024-11-15 23:34:35 +08:00
|
|
|
time.Sleep(1 * time.Second)
|
2023-12-23 14:33:52 +08:00
|
|
|
}
|