oinume journal

Scratchpad of what I learned

gunicornをローカル開発環境のWebサーバとして使う

--reloadオプションがバージョン19.0からサポートされたので、下記のような感じで起動するとローカルの開発サーバとして使える。

$ gunicorn --access-logfile - --log-file - --reload -b 127.0.0.1:3000 -w 1 app:app
  • --access-logfile: "-"を指定するとstderrにアクセスログが出る
  • --log-file: ↑と同様に"-"を指定するとstderrにエラーログが出る
  • --reload: ソースコードが変更されたらリロードする
  • -b: bindするアドレスとポートの指定
  • -w: ワーカー数

Python文法詳解

Python文法詳解

Goでnilなsliceやmapを返すと空のsliceやmapになる

タイトルの通りだけど知らなかったので。明示的に空のsliceやmapを作らなくてもいいのは楽だ。

http://play.golang.org/p/veOWHGDdcn

package main

import "fmt"

func emptyMap() map[string]string {
    return nil
}

func emptySlice() []string {
    return nil
}

func main() {
    emptySlice := emptySlice()
    fmt.Printf("slice len = %d\n", len(emptySlice))

    emptyMap := emptyMap()
    fmt.Printf("map len = %d\n", len(emptyMap))
}

基礎からわかる Go言語

基礎からわかる Go言語

golintの-min_confidenceでチェックするレベルを変更する

これはGo的にはあまりやらない方がいいと思うやつなんだけど、こういうこともできるよ、という紹介。

type Hoge struct {
    Url    string
}

みたいに書いてgolintを実行すると "struct field Url should be URL" と怒られる。しかし、golintには -min_confidence という引数を指定できる。デフォルトは0.8なので、試しにこれを1.0に引き上げてみると"struct field Url should be URL"というエラーは出なくなる。https://github.com/golang/lint/blob/master/lint.go のソースを見てみると、いろいろなチェック処理で

f.errorf(vs, 1, link(docCommentsLink), category("comments"), "exported %s %s should have comment%s or be unexported", kind, name, block)

のようにf.errorfの第2引数にConfidenceを指定しているので、該当のエラーを消したい場合は -min_confidence でそのチェック処理のConfidenceより大きい物を指定すれば良さそう。

package main

type Session struct {
    Url string
}

func (s *Session) Open() bool {
    return true
}

func main() {
    s := Session{"https://github.com/"}
    s.Open()
}

ためしにこんな感じのソースをgolintにかけるとデフォルトでは

$ golint golint.go
golint/golint.go:3:6: exported type Session should have comment or be unexported
golint/golint.go:7:1: exported method Session.Open should have comment or be unexported
golint/golint.go:4:2: struct field Url should be URL

というエラーが出るけど、-min_confidence=1.1にすると全部出なくなる。あんまりやらない方がいいことだとは思うけど、エラー出まくるからという理由でgolintをかけなくなるよりかは -min_confidence を指定してgolintする方がいいのではないかなぁと思った次第です。

The Way to Go: A Thorough Introduction to the Go Programming Language

The Way to Go: A Thorough Introduction to the Go Programming Language

Revel Frameworkでのテスト

今使っているRevel Frameworkのテストについてまとめてみた。

デモ

サンプル https://github.com/oinume/revel-sample

テストの実行(ブラウザー経由)

revel run revel-sample

http://localhost:9000/@tests にアクセス

テストの実行(CUI)

revel test revel-sample

TestSuite

type AppTest struct {
    revel.TestSuite
}

revel.TestSuiteを埋め込んで使うと、コイツが持っている各種関数が使えるようになる。

  • t.Get(PATH)
  • t.AssertOk()
  • t.AssertContentType
  • etc...
type AppTest struct {
    revel.TestSuite
}

func (t *AppTest) Before() {
    //println("Set up")
}

// Test function must start with 'Test'
func (t AppTest) TestThatIndexPageWorks() {
    t.Get("/")
    t.AssertOk() // Check HTTP status is 200
    t.AssertContentType("text/plain; charset=utf-8")
    t.AssertContains("Hello world")
}

func (t *AppTest) After() {
    //println("Tear down")
}

Before/After

  • テスト関数を実行する前/後に呼び出される。
  • テスト関数が呼び出されるたびに呼ばれるので、テストで使うデータのリセットなどを行う用途に使う。

どういうモデルで動作しているか?

TestSuite(HTTP Client)  --> Revel app (Server)

revel test を実行すると裏でサーバーが立ち上がって、TestSuiteがクライアントとしてHTTPリクエストを投げてる。

厳密には...

となっている。

よくある質問

特定のテストだけ実行したい

$ revel test revel-sample AppTest.TestThatIndexPageWorks

引数に <TestSuite名>.<Test関数名> という形式で渡せばそのテスト関数だけ実行される

テスト時のサーバー側のログをあとで確認したい

test-results/app.log にログファイルあるよ!

Mastering Concurrency in Go

Mastering Concurrency in Go

Atomで突然の死ジェネレータを作ってみた

https://atom.io/packages/sudden-death-generator

Atomのpackageを作る練習として。

実践Node.jsプログラミング (Programmer's SELECTION)

実践Node.jsプログラミング (Programmer's SELECTION)