This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package di | |
import ( | |
"bytes" | |
"io" | |
"testing" | |
) | |
// Watcher takes an io.Writer as an attribute | |
type Watcher struct { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
// An interface for an abstraction between structs | |
type StringWriter interface { | |
Write(string) | |
} | |
// LogWriter now implements StringWriter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
// This violates Dependency Inversion Principle | |
type LogWriter struct { | |
Counter int | |
} | |
func (w *LogWriter) Write(msg string) { | |
log.Println(msg) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func countingSort(numbers []int, maxValue int) []int { | |
// Allocate a numCounts slice with cap of the maxValue + 1 | |
// Make sure it's allocated (zero-filled) and not empty. | |
numCounts := make([]int, maxValue+1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// Very naive answer. | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
n := 0 | |
a := 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Error handling omitted for brevity | |
ts1 := httptest.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello world!") | |
})) | |
ts2 := httptest.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello mars!") | |
})) | |
ts3 := httptest.New(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello pluto!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Error handling omitted for brevity | |
var ( | |
helloMarsHandler = func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello Mars client!") | |
} | |
goodDayHandler = func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Good day client!") | |
} | |
palomaHandler = func(w http.ResponseWriter, r *http.Request) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Each hop to the test server takes 1 second | |
delay := time.Duration(1) * time.Second | |
ts := httptest.NewServer(http.HandlerFunc(handler)) | |
p3 := relay.NewProxy(delay, ts) | |
p2 := relay.NewProxy(delay, p3) | |
p1 := relay.NewProxy(delay, p2) | |
start := time.Now() | |
resp, _ := client.Get(p1.URL) | |
elapsed := time.Since(start) | |
So(elapsed, ShouldAlmostEqual, time.Duration(6) * time.Second) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Error handling omitted for brevity | |
func TestGet(t *testing.T) { | |
Convey("GIVEN the test server", t, func() { | |
ts := httptest.NewServer(http.HandlerFunc(handler)) | |
Convey("GIVEN a slow connection which takes 4s to-from the server", func() { | |
// this delay is per trip, meaning it doubles for a round trip | |
delay := time.Duration(2) * time.Second | |
conn := relay.NewProxy(delay, ts) | |
Convey("WITH a client that times out after 3s", func() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"testing" | |
. "github.com/smartystreets/goconvey/convey" | |
) | |
func IncrementInt(i int) int { | |
return i + 1 | |
} | |
func MyTest(t *testing.T) { |