当前位置: 首页>>代码示例>>Golang>>正文


Golang ensure.Nil函数代码示例

本文整理汇总了Golang中github.com/daaku/rell/internal/github.com/facebookgo/ensure.Nil函数的典型用法代码示例。如果您正苦于以下问题:Golang Nil函数的具体用法?Golang Nil怎么用?Golang Nil使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Nil函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestHTTPWithinStopTimeout

func TestHTTPWithinStopTimeout(t *testing.T) {
	t.Parallel()
	hello := []byte("hello")
	finOkHandler := make(chan struct{})
	okHandler := func(w http.ResponseWriter, r *http.Request) {
		defer close(finOkHandler)
		w.WriteHeader(200)
		w.Write(hello)
	}

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	ensure.Nil(t, err)
	server := &http.Server{Handler: http.HandlerFunc(okHandler)}
	transport := &http.Transport{}
	client := &http.Client{Transport: transport}
	down := &httpdown.HTTP{StopTimeout: time.Minute}
	s := down.Serve(server, listener)
	res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String()))
	ensure.Nil(t, err)

	finStop := make(chan struct{})
	go func() {
		defer close(finStop)
		ensure.Nil(t, s.Stop())
	}()

	actualBody, err := ioutil.ReadAll(res.Body)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, actualBody, hello)
	ensure.Nil(t, res.Body.Close())
	<-finOkHandler
	<-finStop
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:33,代码来源:httpdown_test.go

示例2: TestExistingConnState

func TestExistingConnState(t *testing.T) {
	t.Parallel()
	hello := []byte("hello")
	fin := make(chan struct{})
	okHandler := func(w http.ResponseWriter, r *http.Request) {
		defer close(fin)
		w.Write(hello)
	}

	var called int32
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	ensure.Nil(t, err)
	server := &http.Server{
		Handler: http.HandlerFunc(okHandler),
		ConnState: func(c net.Conn, s http.ConnState) {
			atomic.AddInt32(&called, 1)
		},
	}
	transport := &http.Transport{}
	client := &http.Client{Transport: transport}
	down := &httpdown.HTTP{}
	s := down.Serve(server, listener)
	res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String()))
	ensure.Nil(t, err)
	actualBody, err := ioutil.ReadAll(res.Body)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, actualBody, hello)
	ensure.Nil(t, res.Body.Close())

	ensure.Nil(t, s.Stop())
	<-fin

	ensure.True(t, atomic.LoadInt32(&called) > 0)
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:34,代码来源:httpdown_test.go

示例3: TestNewRequestAfterStop

func TestNewRequestAfterStop(t *testing.T) {
	t.Parallel()
	const count = 10000
	hello := []byte("hello")
	finOkHandler := make(chan struct{})
	unblockOkHandler := make(chan struct{})
	okHandler := func(w http.ResponseWriter, r *http.Request) {
		defer close(finOkHandler)
		w.WriteHeader(200)
		const diff = 500
		for i := 0; i < count-diff; i++ {
			w.Write(hello)
		}
		<-unblockOkHandler
		for i := 0; i < diff; i++ {
			w.Write(hello)
		}
	}

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	listener, onClose := NewOnCloseListener(listener)
	ensure.Nil(t, err)
	server := &http.Server{Handler: http.HandlerFunc(okHandler)}
	transport := &http.Transport{}
	client := &http.Client{Transport: transport}
	down := &httpdown.HTTP{}
	s := down.Serve(server, listener)
	res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String()))
	ensure.Nil(t, err)

	finStop := make(chan struct{})
	go func() {
		defer close(finStop)
		ensure.Nil(t, s.Stop())
	}()

	// Wait until the listener is closed.
	<-onClose

	// Now the next request should not be able to connect as the listener is
	// now closed.
	_, err = client.Get(fmt.Sprintf("http://%s/", listener.Addr().String()))

	// We should just get "connection refused" here, but sometimes, very rarely,
	// we get a "connection reset" instead. Unclear why this happens.
	ensure.Err(t, err, regexp.MustCompile("(connection refused|connection reset by peer)$"))

	// Unblock the handler and ensure we finish writing the rest of the body
	// successfully.
	close(unblockOkHandler)
	actualBody, err := ioutil.ReadAll(res.Body)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, actualBody, bytes.Repeat(hello, count))
	ensure.Nil(t, res.Body.Close())
	<-finOkHandler
	<-finStop
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:57,代码来源:httpdown_test.go

示例4: TestDoubleStop

func TestDoubleStop(t *testing.T) {
	t.Parallel()
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	ensure.Nil(t, err)
	server := &http.Server{}
	down := &httpdown.HTTP{}
	s := down.Serve(server, listener)
	ensure.Nil(t, s.Stop())
	ensure.Nil(t, s.Stop())
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:10,代码来源:httpdown_test.go

示例5: TestHTTPStopTimeoutMissed

func TestHTTPStopTimeoutMissed(t *testing.T) {
	t.Parallel()

	klock := clock.NewMock()

	const count = 10000
	hello := []byte("hello")
	finOkHandler := make(chan struct{})
	unblockOkHandler := make(chan struct{})
	okHandler := func(w http.ResponseWriter, r *http.Request) {
		defer close(finOkHandler)
		w.Header().Set("Content-Length", fmt.Sprint(len(hello)*count))
		w.WriteHeader(200)
		for i := 0; i < count/2; i++ {
			w.Write(hello)
		}
		<-unblockOkHandler
		for i := 0; i < count/2; i++ {
			w.Write(hello)
		}
	}

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	ensure.Nil(t, err)
	server := &http.Server{Handler: http.HandlerFunc(okHandler)}
	transport := &http.Transport{}
	client := &http.Client{Transport: transport}
	down := &httpdown.HTTP{
		StopTimeout: time.Minute,
		Clock:       klock,
	}
	s := down.Serve(server, listener)
	res, err := client.Get(fmt.Sprintf("http://%s/", listener.Addr().String()))
	ensure.Nil(t, err)

	finStop := make(chan struct{})
	go func() {
		defer close(finStop)
		ensure.Nil(t, s.Stop())
	}()

	klock.Wait(clock.Calls{After: 1}) // wait for Stop to call After
	klock.Add(down.StopTimeout)

	_, err = ioutil.ReadAll(res.Body)
	ensure.Err(t, err, regexp.MustCompile("^unexpected EOF$"))
	ensure.Nil(t, res.Body.Close())
	close(unblockOkHandler)
	<-finOkHandler
	<-finStop
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:51,代码来源:httpdown_test.go

示例6: TestHTTPServeError

func TestHTTPServeError(t *testing.T) {
	t.Parallel()
	expectedError := errors.New("foo")
	listener, err := net.Listen("tcp", "127.0.0.1:0")
	errChan := make(chan error)
	listener = &acceptErrListener{Listener: listener, err: errChan}
	ensure.Nil(t, err)
	server := &http.Server{}
	down := &httpdown.HTTP{}
	s := down.Serve(server, listener)
	errChan <- expectedError
	ensure.DeepEqual(t, s.Wait(), expectedError)
	ensure.Nil(t, s.Stop())
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:14,代码来源:httpdown_test.go

示例7: TestMuxContextMakerError

func TestMuxContextMakerError(t *testing.T) {
	givenErr := errors.New("")
	var actualErr error
	mux, err := ctxmux.New(
		ctxmux.MuxContextMaker(func(r *http.Request) (context.Context, error) {
			return nil, givenErr
		}),
		ctxmux.MuxErrorHandler(
			func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
				ensure.DeepEqual(t, ctx, context.Background())
				actualErr = err
			}),
	)
	ensure.Nil(t, err)
	hw := httptest.NewRecorder()
	hr := &http.Request{
		Method: "GET",
		URL: &url.URL{
			Path: "/",
		},
	}
	mux.GET(hr.URL.Path, func(context.Context, http.ResponseWriter, *http.Request) error {
		panic("not reached")
	})
	mux.ServeHTTP(hw, hr)
	ensure.DeepEqual(t, actualErr, givenErr)
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:27,代码来源:ctxmux_test.go

示例8: TestServerAbort

func TestServerAbort(t *testing.T) {
	t.Parallel()
	for _, code := range []int{200, 500} {
		server := httptest.NewServer(
			http.HandlerFunc(
				func(w http.ResponseWriter, r *http.Request) {
					w.Header().Add("Content-Length", "4000")
					w.WriteHeader(code)
					w.Write(bytes.Repeat([]byte("a"), 3000))
				},
			),
		)

		u, err := url.Parse(server.URL)
		ensure.Nil(t, err)

		c := &parse.Client{
			Credentials: defaultRestAPIKey,
			BaseURL:     u,
		}
		res := make(map[string]interface{})
		_, err = c.Get(nil, res)
		ensure.NotNil(t, err)
		server.CloseClientConnections()
		server.Close()
	}
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:27,代码来源:parse_test.go

示例9: TestServeCombinedURLWithExt

func TestServeCombinedURLWithExt(t *testing.T) {
	contents := [][]byte{
		[]byte("foo"),
		[]byte("bar"),
	}
	var count int
	h := Handler{
		Path: "/",
		Box: funcBox(func(name string) ([]byte, error) {
			defer func() { count++ }()
			return contents[count], nil
		}),
	}
	v, err := h.URL("n1.js", "n2")
	ensure.Nil(t, err)
	ensure.DeepEqual(t, v, "/W1sibjEuanMiLCJhY2JkMThkYiJdLFsibjIiLCIzN2I1MWQxOSJdXQ.js")
	w := httptest.NewRecorder()
	r := &http.Request{
		URL: &url.URL{
			Path: v,
		},
	}
	h.ServeHTTP(w, r)
	ensure.DeepEqual(t, w.Code, http.StatusOK)
	ensure.DeepEqual(t, w.Body.String(), "foobar")
	ensure.DeepEqual(t, w.Header(), http.Header{
		"Content-Length": []string{"6"},
		"Cache-Control":  []string{cacheControl},
		"Content-Type":   []string{"application/javascript"},
	})
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:31,代码来源:static_test.go

示例10: TestServerAbort

func TestServerAbort(t *testing.T) {
	t.Parallel()
	for _, code := range []int{200, 500} {
		server := httptest.NewServer(
			http.HandlerFunc(
				func(w http.ResponseWriter, r *http.Request) {
					w.Header().Add("Content-Length", "4000")
					w.WriteHeader(code)
					w.Write(bytes.Repeat([]byte("a"), 3000))
				},
			),
		)

		u, err := url.Parse(server.URL)
		ensure.Nil(t, err)

		c := &fbapi.Client{
			Transport: defaultHTTPTransport,
			BaseURL:   u,
		}
		res := make(map[string]interface{})
		_, err = c.Do(&http.Request{Method: "GET"}, res)
		ensure.NotNil(t, err)
		ensure.StringContains(t, err.Error(), fmt.Sprintf(`GET %s`, server.URL))
		server.CloseClientConnections()
		server.Close()
	}
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:28,代码来源:client_test.go

示例11: TestDecodeMultiple

func TestDecodeMultiple(t *testing.T) {
	value, err := decode("W1siZm9vMSIsImJhcjEiXSxbImZvbzIiLCJiYXIyIl1d")
	ensure.Nil(t, err)
	ensure.DeepEqual(t, value, []file{
		{Name: "foo1", Hash: "bar1"},
		{Name: "foo2", Hash: "bar2"},
	})
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:8,代码来源:static_test.go

示例12: TestNilStruct

func TestNilStruct(t *testing.T) {
	var c capture
	ensure.Nil(&c, typ{})
	c.Equal(t, `ensure_test.go:131: expected nil value but got:
(ensure_test.typ) {
 Answer: (int) 0
}`)
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:8,代码来源:ensure_test.go

示例13: TestEncodeMultiple

func TestEncodeMultiple(t *testing.T) {
	files := []file{
		{Name: "foo1", Hash: "bar1"},
		{Name: "foo2", Hash: "bar2"},
	}
	value, err := encode(files)
	ensure.Nil(t, err)
	ensure.DeepEqual(t, value, "W1siZm9vMSIsImJhcjEiXSxbImZvbzIiLCJiYXIyIl1d")
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:9,代码来源:static_test.go

示例14: TestExplicitAreIgnored

func TestExplicitAreIgnored(t *testing.T) {
	const name = "TestExplicitAreIgnored"
	s := flag.NewFlagSet(name, flag.PanicOnError)
	const bar = int(43)
	barActual := s.Int("bar", 0, "")
	s.Parse([]string{"-bar", fmt.Sprint(bar)})
	os.Setenv(named(name, "bar"), "44")
	ensure.Nil(t, flagenv.ParseSet(name, s))
	ensure.DeepEqual(t, *barActual, bar)
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:10,代码来源:flagenv_test.go

示例15: TestSimple

func TestSimple(t *testing.T) {
	t.Parallel()
	var out bytes.Buffer
	w := htmlwriter.New(&out)
	const original = "hello & world"
	n, err := w.Write([]byte(original))
	ensure.Nil(t, err)
	ensure.DeepEqual(t, n, 17)
	ensure.DeepEqual(t, out.String(), html.EscapeString(original))
}
开发者ID:chrisp-fb,项目名称:rell,代码行数:10,代码来源:htmlwriter_test.go


注:本文中的github.com/daaku/rell/internal/github.com/facebookgo/ensure.Nil函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。