Skip to content

Instantly share code, notes, and snippets.

@quii
quii / blah.md
Last active Aug 9, 2022
Learn Go with Tests: Intro to acceptance tests
View blah.md

Learn Go with Tests: Intro to acceptance tests

We've been running into the need to have "graceful shutdown" for our services.

Graceful shutdown is making sure your system finishes its work properly before it is terminated. A real-world analogy would be someone trying to wrap-up a phone call properly before moving on to the next meeting, rather than just hanging-up mid-sentence.

This post will give an intro to graceful shutdown in the context of a HTTP server, and how to write "acceptance tests" to give yourself confidence in the behaviour of your code.

After reading this you'll know how to share packages with excellent tests, reduce maintenance efforts and, increase confidence in the quality of your work.

@quii
quii / arrange_the_furniture.md
Last active Feb 14, 2022
Furniture arranging checklist
View arrange_the_furniture.md

Refactoring step, starting checklist

Refactoring (or as i often annoyingly call it, "Furniture arranging") is a skill that once practiced enough, becomes second-nature, and in a lot of cases, very easy.

It often gets conflated with design, but they are separate activities.

Refactoring vs Design

Refactoring is just improving existing code and not changing behaviour, and it's usually very localised changes; tests shouldn't have to change. A lot of very helpful refactorings are simple to learn, easy to do (many are almost entirely automated by your IDE) but over time become hugely impactful to the quality of our system.

@quii
quii / generics.go
Created Dec 15, 2021
learning afternoon code
View generics.go
package main
import "fmt"
func main() {
someNumbers := []int64{1, 2, 3}
someNames := []string{"John", "Chris", "Mary"}
fmt.Println(Reduce(someNumbers, 0, func(a, b int64) int64 {
return a + b
View testdoubles.md

Words are important

Chris James 15 Jul 2020 https://quii.dev

"Mocking"

People say

@quii
quii / oops_test.go
Last active May 13, 2020
Why does this test pass huh?
View oops_test.go
package learn_go_with_tests
import (
"fmt"
"testing"
)
func Add(x, y int) int {
return 3
}
View es.md

Aggregates

Let's say I have a funds index with 2 documents like this

{
  "fundId": 123,
  "fundManager": {
    "id": 123434,
 "name": "Chris Inc"
View stitcher_test.go
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
@quii
quii / stitcher.go
Created Feb 14, 2020
concurrent stitcher
View stitcher.go
func Stitcher(w http.ResponseWriter, r *http.Request) {
urls := r.URL.Query()["url"]
if len(urls) == 0 {
http.Error(w, "please provide a url querystring value", http.StatusBadRequest)
return
}
bodies := make(chan io.ReadCloser, len(urls))
View stitcher.go
func Stitcher(w http.ResponseWriter, r *http.Request) {
urls := r.URL.Query()["url"]
if len(urls) == 0 {
http.Error(w, "please provide a url querystring value", http.StatusBadRequest)
return
}
for _, url := range urls {
res, _ := http.Get(url)
View gist:ac5946d022fd365eda47e96b3b1b264a
# frozen_string_literal: true
require_relative './setup_test_database'
ENV['RACK_ENV'] = 'test'
ENV['ENVIRONMENT'] = 'test'
# Bring in the contents of the `app.rb` file
require File.join(File.dirname(__FILE__), '..', 'app.rb')