Skip to content

Commit

Permalink
start mapping out sse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Oct 24, 2024
1 parent 2fe0cb3 commit bb4d78d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions languages/go/go-server/sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (controller *defaultSseController[T]) startStream() {
controller.headersSent = true
controller.pingTicker = time.NewTicker(time.Second * 10)
go func() {
defer controller.pingTicker.Stop()
for {
select {
case <-controller.pingTicker.C:
Expand Down
3 changes: 1 addition & 2 deletions playground/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Message struct {
func WatchMessages(params WatchMessagesParams, controller arri.SseController[Message], context arri.DefaultContext) arri.RpcError {
// create ticker that fires each second
t := time.NewTicker(time.Second)
defer t.Stop()
msgCount := 0
for {
select {
Expand All @@ -40,8 +41,6 @@ func WatchMessages(params WatchMessagesParams, controller arri.SseController[Mes
CreatedAt: time.Now(),
})
case <-controller.Done():
// cleanup when the connection is closed
t.Stop()
return nil
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/servers/go/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -295,6 +296,39 @@ func SendRecursiveUnion(params RecursiveUnion, _ AppContext) (RecursiveUnion, ar
return params, nil
}

type AutoReconnectParams struct {
MessageCount uint8
}

type AutoReconnectResponse struct {
Count uint8
Message string
}

func StreamAutoReconnect(params AutoReconnectParams, controller arri.SseController[AutoReconnectResponse], ctx AppContext) arri.RpcError {
t := time.NewTicker(time.Millisecond)
_, cancel := context.WithCancel(ctx.request.Context())
defer t.Stop()
var msgCount uint8 = 0
for {
select {
case <-t.C:
msgCount++
controller.Push(AutoReconnectResponse{Count: msgCount, Message: "Hello World " + string(msgCount)})
if msgCount == params.MessageCount {
cancel()
return nil
}
if msgCount > params.MessageCount {
panic("Request was not properly cancelled")
}
case <-controller.Done():
return nil
}

}
}

type ChatMessageParams struct {
ChannelId string
}
Expand Down

0 comments on commit bb4d78d

Please sign in to comment.