diff --git a/server.go b/server.go index 3a44e5c..2d6d181 100644 --- a/server.go +++ b/server.go @@ -256,14 +256,15 @@ func (srv *Server) Run(handler Handler) error { // Start returns any error encountered during server boot time. // If the server has already been stopped, ErrServerStopped is returned. func (srv *Server) Start(handler Handler) error { - // TODO: Retrun error if handler is nil + if handler == nil { + return fmt.Errorf("asynq: server cannot run with nil handler") + } switch srv.ss.Status() { case base.StatusRunning: return fmt.Errorf("asynq: the server is already running") case base.StatusStopped: return ErrServerStopped } - // TODO: Return error if cannot connect to Redis srv.ss.SetStatus(base.StatusRunning) srv.processor.handler = handler diff --git a/server_test.go b/server_test.go index 9c7188e..cb56ad4 100644 --- a/server_test.go +++ b/server_test.go @@ -50,6 +50,41 @@ func TestServer(t *testing.T) { srv.Stop() } +func TestServerErrServerStopped(t *testing.T) { + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + handler := NewServeMux() + if err := srv.Start(handler); err != nil { + t.Fatal(err) + } + srv.Stop() + err := srv.Start(handler) + if err != ErrServerStopped { + t.Errorf("Restarting server: (*Server).Start(handler) = %v, want ErrServerStopped error", err) + } +} + +func TestServerErrNilHandler(t *testing.T) { + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + err := srv.Start(nil) + if err == nil { + t.Error("Starting server with nil handler: (*Server).Start(nil) did not return error") + srv.Stop() + } +} + +func TestServerErrServerRunning(t *testing.T) { + srv := NewServer(RedisClientOpt{Addr: ":6379"}, Config{}) + handler := NewServeMux() + if err := srv.Start(handler); err != nil { + t.Fatal(err) + } + err := srv.Start(handler) + if err == nil { + t.Error("Calling (*Server).Start(handler) on already running server did not return error") + } + srv.Stop() +} + func TestGCD(t *testing.T) { tests := []struct { input []int