// NewServer serves the given state by accepting requests on the given // listener, using the given certificate and key (in PEM format) for // authentication. func NewServer(s *state.State, addr string, cert, key []byte, datadir, logDir string) (*Server, error) { lis, err := net.Listen("tcp", addr) if err != nil { return nil, err } logger.Infof("listening on %q", lis.Addr()) tlsCert, err := tls.X509KeyPair(cert, key) if err != nil { return nil, err } srv := &Server{ state: s, addr: lis.Addr(), dataDir: datadir, logDir: logDir, limiter: utils.NewLimiter(loginRateLimit), } // TODO(rog) check that *srvRoot is a valid type for using // as an RPC server. lis = tls.NewListener(lis, &tls.Config{ Certificates: []tls.Certificate{tlsCert}, }) go srv.run(lis) return srv, nil }
func (*limiterSuite) TestAcquireWaitBlocksUntilRelease(c *gc.C) { l := utils.NewLimiter(2) calls := make([]string, 0, 10) start := make(chan bool, 0) waiting := make(chan bool, 0) done := make(chan bool, 0) go func() { <-start calls = append(calls, fmt.Sprintf("%v", l.Acquire())) calls = append(calls, fmt.Sprintf("%v", l.Acquire())) calls = append(calls, fmt.Sprintf("%v", l.Acquire())) waiting <- true l.AcquireWait() calls = append(calls, "waited") calls = append(calls, fmt.Sprintf("%v", l.Acquire())) done <- true }() // Start the routine, and wait for it to get to the first checkpoint start <- true select { case <-waiting: case <-time.After(coretesting.LongWait): c.Fatalf("timed out waiting for 'waiting' to trigger") } c.Check(l.Acquire(), jc.IsFalse) l.Release() select { case <-done: case <-time.After(coretesting.LongWait): c.Fatalf("timed out waiting for 'done' to trigger") } c.Check(calls, gc.DeepEquals, []string{"true", "true", "false", "waited", "false"}) }
func (*limiterSuite) TestAcquireAndRelease(c *gc.C) { l := utils.NewLimiter(2) c.Check(l.Acquire(), jc.IsTrue) c.Check(l.Acquire(), jc.IsTrue) c.Check(l.Acquire(), jc.IsFalse) c.Check(l.Release(), gc.IsNil) c.Check(l.Acquire(), jc.IsTrue) c.Check(l.Release(), gc.IsNil) c.Check(l.Release(), gc.IsNil) c.Check(l.Release(), gc.ErrorMatches, "Release without an associated Acquire") }
func (*limiterSuite) TestBadRelease(c *gc.C) { l := utils.NewLimiter(2) c.Check(l.Release(), gc.ErrorMatches, "Release without an associated Acquire") }
func (*limiterSuite) TestAcquireUntilFull(c *gc.C) { l := utils.NewLimiter(2) c.Check(l.Acquire(), jc.IsTrue) c.Check(l.Acquire(), jc.IsTrue) c.Check(l.Acquire(), jc.IsFalse) }