Skip to content

Commit

Permalink
fake example plus license
Browse files Browse the repository at this point in the history
  • Loading branch information
sethgrid committed Dec 1, 2024
1 parent 62a6816 commit ea9ee62
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Seth Ammons

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.
12 changes: 12 additions & 0 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net/http"

Expand Down Expand Up @@ -39,3 +40,14 @@ func RandomFailure() error {
}
return nil
}

// DoSomethingWithEvents is for illustrative purposes of faking during tests,
// showing how faked dependencies bubble up in test assertions
func (s *Server) DoSomethingWithEvents() error {
err := s.eventStore.Write(180, "a message in a bottle")
if err != nil {
s.parentLogger.Error(err.Error())
return fmt.Errorf("unable to DoSomethingWithEvents: %w", err)
}
return nil
}
16 changes: 12 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
mysql "github.com/go-sql-driver/mysql"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/sethgrid/helloworld/events"
"github.com/sethgrid/helloworld/logger"
"github.com/sethgrid/helloworld/taskqueue"
)
Expand All @@ -33,11 +34,17 @@ type contextKey string

var ctxUser contextKey = "user"

type eventWriter interface {
Write(userID int64, message string) error
Close() error
}

type Server struct {
config Config
taskq taskqueue.Tasker
addr string
protocol string
config Config
taskq taskqueue.Tasker
eventStore eventWriter
addr string
protocol string

mu sync.Mutex
started bool
Expand Down Expand Up @@ -128,6 +135,7 @@ func New(conf Config) (*Server, error) {
protocol: protocol,
inDebug: conf.EnableDebug,
taskq: taskqueue.NewMySQLTaskQueue(db, rootLogger, 3, 30*time.Second),
eventStore: events.NewUserEvent(db, 2, rootLogger),
}, nil
}

Expand Down
32 changes: 32 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"bytes"
"fmt"
"io"
"log/slog"
Expand All @@ -25,6 +26,24 @@ func TestHealthcheck(t *testing.T) {
assert.Equal(t, resp.StatusCode, http.StatusOK)
}

func TestEventStoreErr(t *testing.T) {
var logbuf bytes.Buffer

srv, err := newTestServer(&logbuf)
require.NoError(t, err)
defer srv.Close()

// replacet the event store
srv.eventStore = &fakeEventStore{err: fmt.Errorf("oh noes, mysql err")}

err = srv.DoSomethingWithEvents()

require.Error(t, err)
assert.Contains(t, err.Error(), "oh noes, mysql err")

require.Contains(t, logbuf.String(), "oh noes, mysql err")
}

// newTestServer is generally called with no parameter. A bit of a hack on variadics, but if you want to pass in a buffer, pass one in.
// var buf bytes.Buffer
// newTestServer(buf)
Expand All @@ -49,6 +68,7 @@ func newTestServer(logWriter ...io.Writer) (*Server, error) {
protocol: "http://",
taskq: q,
parentLogger: log,
eventStore: &fakeEventStore{},

mu: sync.Mutex{},
}
Expand All @@ -75,3 +95,15 @@ func newTestServer(logWriter ...io.Writer) (*Server, error) {
return srv, nil

}

type fakeEventStore struct {
err error
}

func (f *fakeEventStore) Write(userID int64, message string) error {
return f.err
}

func (f *fakeEventStore) Close() error {
return nil
}

0 comments on commit ea9ee62

Please sign in to comment.