Skip to content

Instantly share code, notes, and snippets.

View cep21's full-sized avatar

Jack Lindamood cep21

View GitHub Profile
@cep21
cep21 / simulation.py
Created April 11, 2014 22:37
Odds of predicting a 49.5 % coin is worse than a 50% coin
import random
def simulate(a, b, iterations):
acount = bcount = 0
for i in xrange(iterations):
vala = random.uniform(0, a)
valb = random.uniform(0, b)
if vala > valb:
acount+=1
elif vala < valb:
bcount += 1
75a76
> num_setable = 0;
271c272
< int num_setable = 0;
---
> int num_setable;
@cep21
cep21 / client_test.go
Last active June 30, 2016 17:50
Example client library test
func ItemTest(t *testing.T) {
// Common setup. Abstract this out
// This allows each test to create its own handler by changing handler variable
handler := http.NotFound
hs := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
handler(rw, req)
}))
defer hs.Close()
// Notice I set the base URL of the client to the httptest server
c := Client{
@cep21
cep21 / client_test.go
Last active June 13, 2023 04:54
Stub out RoundTripper to test HTTP client
type roundTripFunc func (r *http.Request) (*http.Response, error)
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return s(r)
}
func TestSword(t *testing.T) {
var c Client
c.Client.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
assert.Equal(t, r.URL.Path, "/v1/item/sword")
@cep21
cep21 / client_integration_test.go
Last active June 30, 2016 22:11
Example integration test of a client
// +build integration
package smiteclient
// Create a file named info.json and put it at the root of your project. That file should have your
// devId and authKey. The file should be inside .gitignore and not checked into git. Then,
// run `go test -v --tags=integration .` to start integration tests using your auth key.
type devInfo struct {
AuthKey string
}
@cep21
cep21 / client.go
Last active July 7, 2016 23:37
Example draining http response
func (c *Client) doRequest(req *http.Request) {
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer func() {
maxCopySize := 2 << 10
io.CopyN(ioutil.Discard, resp.Body, maxCopySize)
resp.Close()
}()
@cep21
cep21 / client.go
Created July 1, 2016 16:01
Example supporting default empty struct
const DefaultBaseURL = "http://api.smitegame.com/"
func (c *Client) urlBase() string {
if c.BaseURL == "" {
return DefaultBaseURL
}
return c.BaseURL
}
@cep21
cep21 / context_in_struct.go
Last active August 17, 2017 17:49
Example of context with message passing
package main
import "fmt"
import "golang.org/x/net/context"
// A message processes parameter and returns the result on responseChan.
// ctx is places in a struct, but this is ok to do.
type message struct {
responseChan chan<- int
parameter string
@cep21
cep21 / ctx_middleware_chain.go
Last active July 25, 2022 10:17
Example of using context.Value() as a middleware chain
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}