DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

Bulletproof code in go

原文链接

The task: building a server that really isn’t allowed to fail, a project where the cost of error is extraordinarily high

TDD is a great methodology that works well for some, but by itself it still isn’t enough. Even worse, TDD instills false confidence in code and may make developers lazy when considering paranoid edge cases.

No part of our confidence in our code can result from a manual QA process. Humans make mistakes. Human attention to detail deteriorates after doing the same mind-numbing task a hundred times in a row.

Tests must be fast. Blazingly fast.

If a test suite takes more than a few seconds to run, developers are likely going to become lazy, pushing code without running it.

Dealing gracefully with errors in runtime is a cornerstone for bulletproof code.

Complexity is the worst enemy of bulletproof code. One of the best ways to deal with complexity is divide and conquer — split the problem into smaller problems and solve each one separately.

面向接口编程,protobuf部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package spec

import ...

var _ = Describe("Sanity", func() {

var (
node services.Node
)

BeforeEach(func() {
node = services.NewNode()
node.Start()
})

AfterEach(func() {
node.Stop()
})

It("should show balances with GET /api/balance", func() {
resp, err := http.Get("http://localhost:8080/api/balance?from=user1")
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
Expect(ResponseBodyAsString(resp)).To(Equal("0"))
})

})

sync.Map

Scafford started project包含各种测试目录部分,利用它来新建相关的project