From 6468195ba062ffd66bcc9fce3fb455255f25b15e Mon Sep 17 00:00:00 2001 From: Rafael Danras Justo Date: Wed, 7 Sep 2016 12:16:38 -0300 Subject: [PATCH] Add a test mode flag The test mode flag allows to unit test the server, disabling the fork and the service restart mechanisms. Resolves #12 --- overseer.go | 3 +++ proc_master.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/overseer.go b/overseer.go index f161ebb..372529d 100644 --- a/overseer.go +++ b/overseer.go @@ -47,6 +47,9 @@ type Config struct { PreUpgrade func(tempBinaryPath string) error //Debug enables all [overseer] logs. Debug bool + //Test disables fork and server restart to make it possible to unit test + //the server. + Test bool //NoWarn disables warning [overseer] logs. NoWarn bool //NoRestart disables all restarts, this option essentially converts diff --git a/proc_master.go b/proc_master.go index 767d2f8..36b789c 100644 --- a/proc_master.go +++ b/proc_master.go @@ -62,6 +62,32 @@ func (mp *master) run() error { mp.fetch() go mp.fetchLoop() } + if mp.Test { + mp.debugf("test mode enabled") + state := State{ + Enabled: true, + ID: hex.EncodeToString(mp.binHash), + StartedAt: time.Now(), + Address: mp.Config.Address, + Addresses: mp.Config.Addresses, + GracefulShutdown: make(chan bool, 1), + BinPath: mp.binPath, + } + state.Listeners = make([]net.Listener, len(mp.slaveExtraFiles)) + for i, extraFile := range mp.slaveExtraFiles { + l, err := net.FileListener(extraFile) + if err != nil { + return fmt.Errorf("failed to inherit file descriptor: %d", i) + } + u := newOverseerListener(l) + state.Listeners[i] = u + } + if len(state.Listeners) > 0 { + state.Listener = state.Listeners[0] + } + mp.Config.Program(state) + return nil + } return mp.forkLoop() }