test(utils): 添加事件驱动模型测试
- 在 sqrt_test.go 中添加了 fastSqr1 测试函数,用于测试事件驱动模型 - 新增了 Event 和 Uint32AsyncEvent 类型用于测试 - 更新了 go.work、go.mod 和
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/badu/bus"
|
||||
"github.com/badu/bus/test_scenarios/fire-and-forget/events"
|
||||
)
|
||||
|
||||
type EmailServiceImpl struct {
|
||||
sb *strings.Builder
|
||||
}
|
||||
|
||||
func NewEmailService(sb *strings.Builder) EmailServiceImpl {
|
||||
result := EmailServiceImpl{sb: sb}
|
||||
bus.Sub(result.OnUserRegisteredEvent)
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *EmailServiceImpl) OnUserRegisteredEvent(e events.UserRegisteredEvent) {
|
||||
s.sb.WriteString(fmt.Sprintf("user %s has registered - sending welcome email message\n", e.UserName))
|
||||
bus.Pub(events.SMSRequestEvent{
|
||||
Number: e.Phone,
|
||||
Message: fmt.Sprintf("%s your user account was created. Check your email for instructions", e.UserName),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package notifications
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/badu/bus"
|
||||
"github.com/badu/bus/test_scenarios/fire-and-forget/events"
|
||||
)
|
||||
|
||||
type SmsServiceImpl struct {
|
||||
sb *strings.Builder
|
||||
}
|
||||
|
||||
func NewSmsService(sb *strings.Builder) SmsServiceImpl {
|
||||
result := SmsServiceImpl{sb: sb}
|
||||
bus.Sub(result.OnSMSSendRequest)
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *SmsServiceImpl) OnSMSSendRequest(event events.SMSRequestEvent) {
|
||||
s.sb.WriteString(fmt.Sprintf("sms sent requested for number %s with message %s\n", event.Number, event.Message))
|
||||
bus.Pub(events.SMSSentEvent{
|
||||
Request: event,
|
||||
Status: "successfully sent",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user