Skip to content

Instantly share code, notes, and snippets.

@jochasinga
jochasinga / di_test.go
Last active June 1, 2016 03:21
Dependency injection in unit testing
package di
import (
"bytes"
"io"
"testing"
)
// Watcher takes an io.Writer as an attribute
type Watcher struct {
package main
import "log"
// An interface for an abstraction between structs
type StringWriter interface {
Write(string)
}
// LogWriter now implements StringWriter
@jochasinga
jochasinga / di_1.go
Last active May 31, 2016 12:30
Sample structs which violate Dependency Inversion Principle
package main
import "log"
// This violates Dependency Inversion Principle
type LogWriter struct {
Counter int
}
func (w *LogWriter) Write(msg string) {
log.Println(msg)
@jochasinga
jochasinga / counting_sort.go
Created May 26, 2016 15:45
Counting sort implementation in Go
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)
@jochasinga
jochasinga / fibonacci_closure.go
Created May 26, 2016 12:57 — forked from tetsuok/fibonacci_closure.go
An answer of the exercise: Fibonacci closure on a tour of Go
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
@jochasinga
jochasinga / proxy_switcher_chain.go
Last active April 5, 2016 16:03
Chaining HTTP test servers, proxies and a switcher
// 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!")
@jochasinga
jochasinga / switcher_example.go
Last active April 5, 2016 16:40
Using Switcher in Go Relay package
// 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) {
@jochasinga
jochasinga / proxychain.go
Created April 5, 2016 15:36
Proxy behing another Proxy in Relay
// 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)
@jochasinga
jochasinga / testing_relay.go
Last active April 5, 2016 16:37
Example of using Relay Proxy to simulate latent connection in testing
// 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() {
@jochasinga
jochasinga / goconvey.go
Last active April 5, 2016 16:30
Simple test example in Goconvey
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func IncrementInt(i int) int {
return i + 1
}
func MyTest(t *testing.T) {