Unit test and reverse proxy in go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Generally, in order to ensure the stability of the entire system, it is often necessary to write a large number of unit tests, such as Java junit,php PHPUnit and so on provide similar functionality. The testing package in Golang provides the functionality of this test, which is handy when combined with the Go Test tool.

Unit tests in Golang not only feature tests, but also performance tests, which are very powerful.

Functional Testing

In the SRC directory of Golang, create a new directory for math, the test directory structure is as follows:

Golang Unit Test Catalog

Fibonacci.go code is as follows, there is a Fibonacci function mainly

Package lib//Fibonacci Sequence//Find the value of nth number func Fibonacci (n Int64) Int64 {if n < 2 {return N}return Fibonacci (n-1) + Fibonacci (n-2)

Fibonacci_test.go is the test file, Golang need to test the file all with "_test" end, the test function is used to start with the code as follows:

Package Libimport ("testing") Func Testfibonacci (t *testing. T) {r: = Fibonacci (Ten) if r! = T.errorf ("Fibonacci" failed. Got%d, expected. ", R)}}

Test this program with the Go test

$ go Test lib ok lib 0.008s

If you are prompted to not find the package, add the code path to the environment variable Gopath.

Can ' t load package:package lib:cannot Find Package "Lib" in any of:

Performance testing

In combination with the above method, the performance of the function is tested here, and if you need to perform a performance test, the function starts with benchmark.

Performance test func Benchmarkfibonacci (b *testing. B) {for i: = 0; i < B.N; i++ {Fibonacci (10)}}

Next, perform this performance test:

$ go test-bench=. Lib PASS benchmarkfibonacci 5000000 436 ns/op ok Lib 2.608s

The second line of output indicates that the function ran 5 million times, and the average time to run it was 436ns.

This performance test only tests the case with a parameter of 10. You can test multiple parameters if you need to:

The performance of the test parameter is 5 func BenchmarkFibonacci5 (b *testing. b) {for i: = 0; i < B.N; i++ {Fibonacci (5)}}//Test parameter is 20 performance func BenchmarkFibonacci20 (b *testing. B) {for i: = 0; i < B.N; i++ {Fibonacci (20)}}

Run it:

$ go test-bench=. Lib PASS benchmarkfibonacci 5000000 357 ns/op BenchmarkFibonacci5 100000000 29.5 ns/op BenchmarkFibonacci20 50000 44688 NS /op OK Lib 7.824s

If there are a lot of ways to test performance, it will take longer. You can set the number of performance test rows that need to run through the-bench= parameter:

$ go test-bench=fibonacci20 lib PASS BenchmarkFibonacci20 50000 44367 ns/op ok Lib 2.677s

Reprint Please specify: Happy Programming»golang Unit Test

Look at the Golang bag manual when you see Net/http/httputil there is a type reverseproxy, this is not a reverse proxy it! Golang comes with reverse proxy function? Curiosity to try, it is very simple, not a few lines of code to achieve a simple reverse proxy service.

About reverse proxy Baidu Encyclopedia said very detailed, here excerpt the definition:

The reverse proxy method refers to a proxy server that accepts connection requests on the Internet, then forwards the request to a server on the internal network and returns the results from the server to the client requesting the connection on the Internet, Reverse. At this point the proxy server appears as a reverse proxy server externally.

Golang Implementation Code

Package Mainimport ("Log", "Net/http" "Net/http/httputil" "Net/url") type handle struct {host Stringport string}func (this * HANDLE) servehttp (w http. Responsewriter, R *http. Request) {remote, err: = URL. Parse ("http://" + This.host + "+" + this.port) if err! = Nil {panic (err)}proxy: = Httputil. Newsinglehostreverseproxy (remote) proxy. Servehttp (W, R)}func StartServer () {//proxy server host and Porth: = &handle{host: "127.0.0.1", Port: "W"}err: = http. Listenandserve (": 8888", h) if err! = Nil {log. Fatalln ("Listenandserve:", err)}}func main () {StartServer ()}

The key code is Newsinglehostreverseproxy this method, look at the source word is not difficult to see the method returned a Reverseproxy object, in Reverseproxy Servehttp method to achieve this specific process, The primary is to reseal the source HTTP header and then send it to the back-end server.

Reproduced please specify: Happy programming»golang implementation of the reverse proxy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.