mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-24 23:02:18 +08:00
Add test cases for server error
This commit is contained in:
parent
239ef27a6e
commit
eef2f5f3cb
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user