Skip to content

Commit

Permalink
Use waiter interface instead of time.Sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
tygern committed Apr 3, 2024
1 parent 6a90156 commit ff45ece
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
10 changes: 9 additions & 1 deletion cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"github.com/initialcapacity/go-streaming/internal/app"
"github.com/initialcapacity/go-streaming/pkg/websupport"
"log"
"time"
)

type oneSecondWaiter struct {
}

func (w oneSecondWaiter) Wait() <-chan time.Time {
return time.After(1 * time.Second)
}

func main() {
host := websupport.EnvironmentVariable("HOST", "")
port := websupport.EnvironmentVariable("PORT", 8777)

server := websupport.NewServer(app.Handlers(true))
server := websupport.NewServer(app.Handlers(oneSecondWaiter{}))

_, done := server.Start(host, port)
log.Fatal(<-done)
Expand Down
4 changes: 2 additions & 2 deletions internal/app/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"net/http"
)

func Handlers(addArtificialDelay bool) func(mux *http.ServeMux) {
func Handlers(delay waiter) func(mux *http.ServeMux) {
return func(mux *http.ServeMux) {
mux.HandleFunc("GET /", Index(addArtificialDelay))
mux.HandleFunc("GET /", Index(delay))
mux.HandleFunc("GET /health", Health)

static, _ := fs.Sub(Resources, "resources/static")
Expand Down
14 changes: 13 additions & 1 deletion internal/app/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ import (
"github.com/initialcapacity/go-streaming/pkg/testsupport"
"github.com/initialcapacity/go-streaming/pkg/websupport"
"testing"
"time"
)

type immediateWaiter struct {
}

func (w immediateWaiter) Wait() <-chan time.Time {
c := make(chan time.Time)
go func() {
c <- time.Now()
}()
return c
}

func TestHealth(t *testing.T) {
server := websupport.NewServer(app.Handlers(false))
server := websupport.NewServer(app.Handlers(immediateWaiter{}))
port, _ := server.Start("localhost", 0)
testsupport.AssertHealthy(t, port, "/health")
}
10 changes: 6 additions & 4 deletions internal/app/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ type model struct {
Message deferrable.Deferrable[[]string]
}

func Index(addArtificialDelay bool) http.HandlerFunc {
type waiter interface {
Wait() <-chan time.Time
}

func Index(delay waiter) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data := make(chan []string)

go func() {
if addArtificialDelay {
time.Sleep(1 * time.Second)
}
<-delay.Wait()
data <- []string{"Here's some slow content.", "It took a while to load.", "And didn't use any javascript."}
close(data)
}()
Expand Down
2 changes: 1 addition & 1 deletion internal/app/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestIndex(t *testing.T) {
address, server := testsupport.StartTestServer(t, app.Handlers(false))
address, server := testsupport.StartTestServer(t, app.Handlers(immediateWaiter{}))
defer testsupport.StopTestServer(t, server)

response, err := http.Get(address)
Expand Down

0 comments on commit ff45ece

Please sign in to comment.