Ejemplo n.º 1
0
func TestCustomPrefix(t *testing.T) {
	Convey("Given the App running with PREFIX", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		f := Flag
		f.Prefix = "BETA"
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", f)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting a message", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte(""))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__prefix\":\"BETA\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

	})

	Convey("Given the App running WITHOUT prefix", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		f := Flag
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", f)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting a message", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			post_body := bytes.NewReader([]byte(""))
			http.Post(ts.URL+"/log/fakelevel/fakecategory/fakeslug/", "application/json", post_body)

			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldNotContainSubstring, "\"__prefix\"")
			So(body, ShouldContainSubstring, "\"__category\":\"fakecategory\"")
			So(body, ShouldContainSubstring, "\"__level\":\"fakelevel\"")
			So(body, ShouldContainSubstring, "\"__namespace\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"fakeslug\"")
		})

	})
}
Ejemplo n.º 2
0
// handleSearch handles URLs like /search?q=golang&timeout=1s by forwarding the
// query to google.Search. If the query param includes timeout, the search is
// canceled after that duration elapses.
func handleSearch(w http.ResponseWriter, req *http.Request) {
	// ctx is the Context for this handler. Calling cancel closes the
	// ctx.Done channel, which is the cancellation signal for requests
	// started by this handler.
	var (
		ctx    context.Context
		cancel context.CancelFunc
	)
	timeout, err := time.ParseDuration(req.FormValue("timeout"))
	if err == nil {
		// The request has a timeout, so create a context that is
		// canceled automatically when the timeout expires.
		ctx, cancel = context.WithTimeout(context.Background(), timeout)
	} else {
		ctx, cancel = context.WithCancel(context.Background())
	}
	defer cancel() // Cancel ctx as soon as handleSearch returns.

	// Check the search query.
	query := req.FormValue("q")
	if query == "" {
		http.Error(w, "no query", http.StatusBadRequest)
		return
	}

	// Store the user IP in ctx for use by code in other packages.
	userIP, err := userip.FromRequest(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	ctx = userip.NewContext(ctx, userIP)

	// Run the Google search and print the results.
	start := time.Now()
	results, err := google.Search(ctx, query)
	elapsed := time.Since(start)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if err := resultsTemplate.Execute(w, struct {
		Results          google.Results
		Timeout, Elapsed time.Duration
	}{
		Results: results,
		Timeout: timeout,
		Elapsed: elapsed,
	}); err != nil {
		log.Print(err)
		return
	}
}
Ejemplo n.º 3
0
Archivo: main.go Proyecto: zoglee/netty
func main() {
	flag.Parse()
	runtime.GOMAXPROCS(runtime.NumCPU() - 1)

	laddr, _ := net.ResolveTCPAddr("tcp", ":"+port) // TODO(zog): flag
	l, err := net.ListenTCP("tcp", laddr)
	if err != nil {
		glog.Fatalf("net.ListenTCP: %v", err)
	}

	ctx := context.Background()

	glog.Infof("server start listen on: %s", laddr.String())
	for {
		tcpconn, err := l.AcceptTCP()
		if err != nil {
			glog.Errorf("Accept error, server stop (listen on:%s), err: %s",
				laddr.String(), err)
			break
		}

		go HandleConnection(ctx, tcpconn)
	}
	glog.Infof("server stop listen on: %s)", laddr.String())
}
Ejemplo n.º 4
0
func TestBlobstore(t *testing.T) {
	db := datasql.MustConnect("sqlite3", ":memory:")
	bs := New(db)

	g := goblin.Goblin(t)
	g.Describe("Blobstore", func() {

		// before each test be sure to purge the blob
		// table data from the database.
		g.Before(func() {
			db.Exec("DELETE FROM blobs")
		})

		g.It("Should Put a Blob", func() {
			err := bs.Put("foo", []byte("bar"))
			g.Assert(err == nil).IsTrue()
		})

		g.It("Should Put a Blob reader", func() {
			var buf bytes.Buffer
			buf.Write([]byte("bar"))
			err := bs.PutReader("foo", &buf)
			g.Assert(err == nil).IsTrue()
		})

		g.It("Should Overwrite a Blob", func() {
			bs.Put("foo", []byte("bar"))
			bs.Put("foo", []byte("baz"))
			blob, err := bs.Get("foo")
			g.Assert(err == nil).IsTrue()
			g.Assert(string(blob)).Equal("baz")
		})

		g.It("Should Get a Blob", func() {
			bs.Put("foo", []byte("bar"))
			blob, err := bs.Get("foo")
			g.Assert(err == nil).IsTrue()
			g.Assert(string(blob)).Equal("bar")
		})

		g.It("Should Get a Blob reader", func() {
			bs.Put("foo", []byte("bar"))
			r, _ := bs.GetReader("foo")
			blob, _ := ioutil.ReadAll(r)
			g.Assert(string(blob)).Equal("bar")
		})

		g.It("Should Del a Blob", func() {
			bs.Put("foo", []byte("bar"))
			err := bs.Del("foo")
			g.Assert(err == nil).IsTrue()
		})

		g.It("Should create a Context", func() {
			c := NewContext(context.Background(), db)
			b := blobstore.FromContext(c).(*Blobstore)
			g.Assert(b.DB).Equal(db)
		})
	})
}
Ejemplo n.º 5
0
func TestHTTP(t *testing.T) {
	ctx := context.Background()
	once.Do(startServer)
	testHTTPRPC(ctx, t, "")
	newOnce.Do(startNewServer)
	testHTTPRPC(ctx, t, newHttpPath)
}
Ejemplo n.º 6
0
func (qre *QueryExecutor) checkPermissions() {
	// Skip permissions check if we have a background context.
	if qre.ctx == context.Background() {
		return
	}

	// Blacklist
	ci := callinfo.FromContext(qre.ctx)
	action, desc := qre.plan.Rules.getAction(ci.RemoteAddr(), ci.Username(), qre.bindVars)
	switch action {
	case QR_FAIL:
		panic(NewTabletError(FAIL, "Query disallowed due to rule: %s", desc))
	case QR_FAIL_RETRY:
		panic(NewTabletError(RETRY, "Query disallowed due to rule: %s", desc))
	}

	// ACLs
	if !qre.plan.Authorized.IsMember(ci.Username()) {
		errStr := fmt.Sprintf("table acl error: %q cannot run %v on table %q", ci.Username(), qre.plan.PlanId, qre.plan.TableName)
		if qre.qe.strictTableAcl {
			panic(NewTabletError(FAIL, "%s", errStr))
		}
		qre.qe.accessCheckerLogger.Errorf("%s", errStr)
	}
}
Ejemplo n.º 7
0
func main() {
	const numRequests = 3
	// create a channel to capture our results
	forecasts := make(chan *openweathermap.Forecast, numRequests)

	// create our channel of requests
	requests := make(chan par.RequestFunc, numRequests)
	requests <- findById(4288809, forecasts) // Covington, VA
	requests <- findById(4288809, forecasts)
	requests <- findById(4140963, forecasts) // DC
	close(requests)                          // important to remember to close the channel

	// resolver := par.Requests(requests).WithRedundancy(1)
	resolver := par.Requests(requests).WithConcurrency(numRequests)
	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	// ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
	err := resolver.DoWithContext(ctx)
	cancel()
	ok(err)

	// the forecasts channel now contains all our forecasts
	close(forecasts)
	cities := map[string]*openweathermap.Forecast{}
	for forecast := range forecasts {
		cities[forecast.Name] = forecast
	}
}
Ejemplo n.º 8
0
// IsHealthy returns nil if the query service is healthy (able to
// connect to the database and serving traffic) or an error explaining
// the unhealthiness otherwise.
func IsHealthy() error {
	return SqlQueryRpcService.Execute(
		context.Background(),
		&proto.Query{Sql: "select 1 from dual", SessionId: SqlQueryRpcService.sessionId},
		new(mproto.QueryResult),
	)
}
Ejemplo n.º 9
0
// BasicMngrHandler can be use in "manager" ServeHTTP after initital required interface like
// authmodel.UserManager, authmodel.GroupManager, conf.Configurator...etc
func BasicMngrHandler(authCtx *AuthContext, rw http.ResponseWriter, req *http.Request, cond *Condition, fn HandleFunc) {
	var cancel context.CancelFunc
	authCtx.Context, cancel = context.WithTimeout(context.Background(), HandleTimeout)
	defer cancel()

	authCtx.req = req
	token := strings.TrimPrefix(req.Header.Get("Authorization"), "Bearer ")
	authCtx.saveToken(token)
	authCtx.saveId(mux.Vars(req)["user_id"])

	authCtx.Notifications = DEFAULT_NOTIFICATOR
	authCtx.Logs = DEFAULT_LOGGER

	rw.Header().Set("Content-Type", "application/json; charset=utf-8")
	if cond.RequiredPri != nil || cond.Owner {
		_, err := authCtx.ValidCurrentUser(cond.Owner, cond.RequiredPri)
		if err != nil {
			JSONError(rw, err.Error(), http.StatusForbidden)
			return
		}
	}

	status, err := fn(authCtx, rw, req)
	if err != nil {
		authCtx.Logs.Errorf("HTTP %d: %q", status, err)
		JSONError(rw, err.Error(), status)
	}
}
Ejemplo n.º 10
0
func TestServeRequest(t *testing.T) {
	ctx := context.Background()
	once.Do(startServer)
	testServeRequest(ctx, t, nil)
	newOnce.Do(startNewServer)
	testServeRequest(ctx, t, newServer)
}
Ejemplo n.º 11
0
func TestRPC(t *testing.T) {
	ctx := context.Background()
	once.Do(startServer)
	testRPC(ctx, t, serverAddr)
	newOnce.Do(startNewServer)
	testRPC(ctx, t, newServerAddr)
}
Ejemplo n.º 12
0
func main() {
	flag.Parse()
	client, err := zrpc.Dial("tcp://127.0.0.1:1337")
	if err != nil {
		log.Fatal(err)
	}

	start := time.Now()
	runs := 10000

	for i := 0; i < runs; i++ {
		// Create the request and response
		req := &pb.ReverseRequest{
			NormalString: proto.String("teststring"),
		}
		resp := &pb.ReverseResponse{}

		// Create the context and pass request timeout and service name
		ctx, _ := context.WithTimeout(context.Background(), time.Second*1)
		ctx = zrpc.NewServiceNameContext(ctx, "reverseservice")
		if err := client.Call(ctx, req, resp); err != nil {
			log.Println("error:", err)
		} else {
			log.Println("received:", resp)
		}

		log.Printf("%d goroutines", runtime.NumGoroutine())
		// time.Sleep(time.Millisecond * 500)
	}

	totalTime := time.Since(start)

	log.Printf("Performed %d reqs in %s (avg %s)", runs, totalTime, totalTime/time.Duration(runs))
}
Ejemplo n.º 13
0
func TestNewAndFromContext(t *testing.T) {
	newl := log.WithField("key", "value")
	ctx := NewContext(context.Background(), newl)
	froml, ok := FromContext(ctx)
	if !ok || newl != froml {
		t.Error("context does not contain tcplog")
	}
}
Ejemplo n.º 14
0
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, RequestTimeout)
	defer cancel()
	ctx = reqid.NewContext(ctx, reqid.New())
	log.Printf(ctx, "http request: remote=%q method=%q url=%q",
		r.RemoteAddr, r.Method, r.URL)
	h(ctx, w, r)
}
Ejemplo n.º 15
0
func SetupIpfs() (*core.IpfsNode, error) {
	// Assume the user has run 'ipfs init'
	r := fsrepo.At("~/.ipfs")
	if err := r.Open(); err != nil {
		return nil, err
	}

	builder := core.NewNodeBuilder().Online().SetRepo(r)
	return builder.Build(context.Background())
}
Ejemplo n.º 16
0
// New creates a new Wrangler object.
//
// actionTimeout: how long should we wait for an action to complete?
// - if using wrangler for just one action, this is set properly
//   upon wrangler creation.
// - if re-using wrangler multiple times, call ResetActionTimeout before
//   every action. Do not use this too much, just for corner cases.
//   It is just much easier to create a new Wrangler object per action.
//
// lockTimeout: how long should we wait for the initial lock to start
// a complex action?  This is distinct from actionTimeout because most
// of the time, we want to immediately know that our action will
// fail. However, automated action will need some time to arbitrate
// the locks.
func New(logger logutil.Logger, ts topo.Server, actionTimeout, lockTimeout time.Duration) *Wrangler {
	ctx, cancel := context.WithTimeout(context.Background(), actionTimeout)
	return &Wrangler{
		logger:      logger,
		ts:          ts,
		tmc:         tmclient.NewTabletManagerClient(),
		ctx:         ctx,
		cancel:      cancel,
		deadline:    time.Now().Add(actionTimeout),
		lockTimeout: lockTimeout,
	}
}
Ejemplo n.º 17
0
func (p *IPFSHandler) Init(repo string) {
	p.repo = fsrepo.At(repo)
	err := p.repo.Open()
	if err != nil {
		panic(err)
	}

	p.node, err = core.NewIPFSNode(context.Background(), core.Online(p.repo))
	if err != nil {
		panic(err)
	}
}
Ejemplo n.º 18
0
func TestPostMany(t *testing.T) {
	Convey("Given the App running", t, func() {
		uc := make(chan string, 0)
		bc := make(chan string, 0)
		f := Flag
		f.BufferSize = 100
		f.BufferTimeout = "100ms"
		ctx, _ := context.WithCancel(context.Background())
		ctx = context.WithValue(ctx, "Flag", f)
		app := NewApp(ctx, GetMockPerformJsonPost(uc, bc))
		v1httpapi := V1HttpApi{
			App: app,
		}
		v1httpapi.registerRoutes(app.Router)

		Convey("When posting BufferSize + 10 messages", func() {
			ts := httptest.NewServer(app.Router)
			defer ts.Close()
			for i := 1; i <= f.BufferSize+10; i++ {
				post_body := bytes.NewReader([]byte("{\"i\":\"" + strconv.Itoa(i) + "\"}"))
				http.Post(ts.URL+"/log/many/at/once/", "application/json", post_body)
			}

			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body := <-bc
			So(body, ShouldContainSubstring, "\"__level\":\"many\"")
			So(body, ShouldContainSubstring, "\"__category\":\"at\"")
			So(body, ShouldContainSubstring, "\"__slug\":\"once\"")
			for i := 1; i <= f.BufferSize; i++ {
				So(body, ShouldContainSubstring, "\"i\":\""+strconv.Itoa(i)+"\"")
			}
			So(<-uc, ShouldEqual, "http://"+f.ApiEndPoint+"/v1/log/bulk/")
			body = <-bc
			for i := f.BufferSize + 1; i <= f.BufferSize+10; i++ {
				So(body, ShouldContainSubstring, "\"i\":\""+strconv.Itoa(i)+"\"")
			}

			body = "EMPTY"
			var uc_value string = "EMPTY"
			select {
			case uc_value = <-uc:
			case <-time.After(time.Second):
			}
			select {
			case body = <-bc:
			case <-time.After(time.Second):
			}
			So(uc_value, ShouldEqual, "EMPTY")
			So(body, ShouldEqual, "EMPTY")
		})

	})
}
Ejemplo n.º 19
0
func ExampleWithTimeout() {
	// Pass a context with a timeout to tell a blocking function that it
	// should abandon its work after the timeout elapses.
	ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
	select {
	case <-time.After(200 * time.Millisecond):
		fmt.Println("overslept")
	case <-ctx.Done():
		fmt.Println(ctx.Err()) // prints "context deadline exceeded"
	}
	// Output:
	// context deadline exceeded
}
Ejemplo n.º 20
0
func (s *BaseSuite) SetUpTest(c *C, clientAddr string) {
	if s.client == nil {
		log.Println("setting up client at:", clientAddr)
		s.client, _ = Dial(clientAddr)
	}

	s.req = &pb.ReverseRequest{
		NormalString: proto.String("test"),
	}
	s.resp = &pb.ReverseResponse{}
	ctx, _ := context.WithTimeout(context.Background(), time.Second*3)
	s.ctx = NewServiceNameContext(ctx, "reverseservice")
}
Ejemplo n.º 21
0
func TestClientWriteError(t *testing.T) {
	ctx := context.Background()
	w := &writeCrasher{done: make(chan bool)}
	client := NewClient(w)
	res := false
	err := client.Call(ctx, "foo", 1, &res)
	if err == nil {
		t.Fatal("expected error")
	}
	if err.Error() != "fake write failure" {
		t.Error("unexpected value of error:", err)
	}
	w.done <- true
}
Ejemplo n.º 22
0
// AgentRpcTestSuite will run the test suite using the provided client and
// the provided tablet. Tablet's vt address needs to be configured so
// the client will connect to a server backed by our RpcAgent (returned
// by NewFakeRpcAgent)
func AgentRpcTestSuite(t *testing.T, client tmclient.TabletManagerClient, ti *topo.TabletInfo) {
	ctx := context.Background()

	// Various read-only methods
	agentRpcTestPing(ctx, t, client, ti)
	agentRpcTestGetSchema(ctx, t, client, ti)
	agentRpcTestGetPermissions(ctx, t, client, ti)

	// Various read-write methods
	agentRpcTestSetReadOnly(ctx, t, client, ti)
	agentRpcTestChangeType(ctx, t, client, ti)
	agentRpcTestScrap(ctx, t, client, ti)
	agentRpcTestSleep(ctx, t, client, ti)
	agentRpcTestExecuteHook(ctx, t, client, ti)
	agentRpcTestRefreshState(ctx, t, client, ti)
	agentRpcTestRunHealthCheck(ctx, t, client, ti)
	agentRpcTestReloadSchema(ctx, t, client, ti)
	agentRpcTestPreflightSchema(ctx, t, client, ti)
	agentRpcTestApplySchema(ctx, t, client, ti)
	agentRpcTestExecuteFetch(ctx, t, client, ti)

	// Replication related methods
	agentRpcTestSlaveStatus(ctx, t, client, ti)
	agentRpcTestWaitSlavePosition(ctx, t, client, ti)
	agentRpcTestMasterPosition(ctx, t, client, ti)
	agentRpcTestReparentPosition(ctx, t, client, ti)
	agentRpcTestStopSlave(ctx, t, client, ti)
	agentRpcTestStopSlaveMinimum(ctx, t, client, ti)
	agentRpcTestStartSlave(ctx, t, client, ti)
	agentRpcTestTabletExternallyReparented(ctx, t, client, ti)
	agentRpcTestGetSlaves(ctx, t, client, ti)
	agentRpcTestWaitBlpPosition(ctx, t, client, ti)
	agentRpcTestStopBlp(ctx, t, client, ti)
	agentRpcTestStartBlp(ctx, t, client, ti)
	agentRpcTestRunBlpUntil(ctx, t, client, ti)

	// Reparenting related functions
	agentRpcTestDemoteMaster(ctx, t, client, ti)
	agentRpcTestPromoteSlave(ctx, t, client, ti)
	agentRpcTestSlaveWasPromoted(ctx, t, client, ti)
	agentRpcTestRestartSlave(ctx, t, client, ti)
	agentRpcTestSlaveWasRestarted(ctx, t, client, ti)
	agentRpcTestBreakSlaves(ctx, t, client, ti)

	// Backup / restore related methods
	agentRpcTestSnapshot(ctx, t, client, ti)
	agentRpcTestSnapshotSourceEnd(ctx, t, client, ti)
	agentRpcTestReserveForRestore(ctx, t, client, ti)
	agentRpcTestRestore(ctx, t, client, ti)
}
Ejemplo n.º 23
0
// contextMiddleware creates a new go.net/context and
// injects into the current goji context.
func contextMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var ctx = context.Background()
		ctx = datasql.NewContext(ctx, db)
		ctx = blobsql.NewContext(ctx, db)
		ctx = pool.NewContext(ctx, workers)
		ctx = director.NewContext(ctx, worker)

		// add the context to the goji web context
		webcontext.Set(c, ctx)
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
Ejemplo n.º 24
0
func TestHandleMultipleConnections(t *testing.T) {
	// to be sent to each connecting clients
	response := []byte("a response\n")

	handle := func(ctx context.Context, conn net.Conn) error {
		conn.Write(response)
		return nil
	}

	// done will close when tcpserver returns
	done := make(chan struct{})

	// start tcp server
	port := ":4444"
	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		err := Listen(ctx, port, handle)
		if err != nil {
			t.Error(err)
		}
		close(done)
	}()

	// make 5 client requests
	for i := 0; i < 5; i++ {
		conn, err := net.Dial("tcp", "localhost"+port)
		if err != nil {
			t.Error(err)
		}

		resp, err := bufio.NewReader(conn).ReadString('\n')
		if err != nil {
			t.Error(err)
		}

		if resp != string(response) {
			t.Error("got %s but expected %s", resp, string(response))
		}
	}

	cancel()

	// wait for tcpserver to cleanup
	select {
	case <-done:
	case <-time.After(3 * time.Second):
		t.Error("tcpserver did not cleanup in time")
	}
}
Ejemplo n.º 25
0
Archivo: server.go Proyecto: htee/htee
func (s *server) handleRequest(w http.ResponseWriter, r *http.Request) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	ctx = newContext(ctx, r)

	switch r.Method {
	case "GET":
		s.playbackStream(ctx, w, r)
	case "POST":
		s.recordStream(ctx, w, r)
	case "DELETE":
		s.deleteStream(ctx, w, r)
	}
}
Ejemplo n.º 26
0
func TestBlobstore(t *testing.T) {
	caps := map[string]bool{}
	caps[Registration] = true

	ctx := NewContext(context.Background(), caps)

	g := goblin.Goblin(t)
	g.Describe("Capabilities", func() {

		g.It("Should get capabilities from context", func() {
			g.Assert(Enabled(ctx, Registration)).Equal(true)
			g.Assert(Enabled(ctx, "Fake Key")).Equal(false)
		})
	})
}
Ejemplo n.º 27
0
// ContextMiddleware creates a new go.net/context and
// injects into the current goji context.
func ContextMiddleware(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var ctx = context.Background()
		ctx = datastore.NewContext(ctx, database.NewDatastore(db))
		ctx = blobstore.NewContext(ctx, database.NewBlobstore(db))
		ctx = pool.NewContext(ctx, workers)
		ctx = director.NewContext(ctx, worker)
		ctx = pubsub.NewContext(ctx, pub)
		ctx = capability.NewContext(ctx, caps)

		// add the context to the goji web context
		webcontext.Set(c, ctx)
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
Ejemplo n.º 28
0
func main() {

	var server string

	var (
		ctx    context.Context
		cancel context.CancelFunc
	)

	ctx, cancel = context.WithCancel(context.Background())

	runtime.GOMAXPROCS(1)

	server = os.Args[1]
	id := os.Args[2]

	LogConditional(printLog, fmt.Printf, "Client Id %s\n", id)
	//fmt.Printf("Client Id %s\n", id)

	var headers http.Header = make(http.Header)
	headers.Add("X-Client-ID", id)

	var srvurl = "ws://173.39.210.210:8080/echo/"

	origin = fmt.Sprintf("http://%s/", server)
	srvurl = fmt.Sprintf("ws://%s:8080/echo/?id=%s", server, id)

	u, err := url.Parse(srvurl)
	o, err := url.Parse(origin)
	ws, err := websocket.DialConfig(&websocket.Config{Location: u, Header: headers, Origin: o, Version: 13})

	if err != nil {
		log.Fatal(err)
	}

	c := make(chan []byte)

	go collectdTcp(cancel, c)
	go collectSyslog(c)
	go writer(ws, id, c)
	//go reader(ws, id)

	select {
	case <-ctx.Done():
	}

}
Ejemplo n.º 29
0
func main() {
	flag.Parse()

	ctx, done := context.WithCancel(context.Background())

	go func() {
		publish(redial(ctx, *url), read(os.Stdin))
		done()
	}()

	go func() {
		subscribe(redial(ctx, *url), write(os.Stdout))
		done()
	}()

	<-ctx.Done()
}
Ejemplo n.º 30
0
func TestSendDeadlock(t *testing.T) {
	ctx := context.Background()
	client := NewClientWithCodec(WriteFailCodec(0))

	done := make(chan bool)
	go func() {
		testSendDeadlock(ctx, client)
		testSendDeadlock(ctx, client)
		done <- true
	}()
	select {
	case <-done:
		return
	case <-time.After(5 * time.Second):
		t.Fatal("deadlock")
	}
}