From f29a512ad9ec6a91c6bf1db3060da53c77b91adb Mon Sep 17 00:00:00 2001 From: 3DRX Date: Thu, 11 Jul 2024 18:26:20 +0800 Subject: [PATCH 1/3] test all branch in getTransporterName --- pkg/route/engine_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/route/engine_test.go b/pkg/route/engine_test.go index b3e0adb30..acb557826 100644 --- a/pkg/route/engine_test.go +++ b/pkg/route/engine_test.go @@ -94,6 +94,9 @@ func TestNew_Engine_WithTransporter(t *testing.T) { func TestGetTransporterName(t *testing.T) { name := getTransporterName(&fakeTransporter{}) assert.DeepEqual(t, "route", name) + + name = getTransporterName(nil) + assert.DeepEqual(t, "unknown", name) } func TestEngineUnescape(t *testing.T) { From 7b1121c9bba2823e83368221138fe46cb91c9e55 Mon Sep 17 00:00:00 2001 From: 3DRX Date: Thu, 11 Jul 2024 18:30:09 +0800 Subject: [PATCH 2/3] Remove dead code --- pkg/route/engine.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/route/engine.go b/pkg/route/engine.go index 30b899dac..d0f4f3f40 100644 --- a/pkg/route/engine.go +++ b/pkg/route/engine.go @@ -295,9 +295,6 @@ func (engine *Engine) Shutdown(ctx context.Context) (err error) { if atomic.LoadUint32(&engine.status) != statusRunning { return errStatusNotRunning } - if !atomic.CompareAndSwapUint32(&engine.status, statusRunning, statusShutdown) { - return - } ch := make(chan struct{}) // trigger hooks if any From 351380978e570b413d5e93358f39f40cb880c003 Mon Sep 17 00:00:00 2001 From: 3DRX Date: Thu, 11 Jul 2024 18:51:09 +0800 Subject: [PATCH 3/3] Add TestMarkAsRunning --- pkg/route/engine_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/route/engine_test.go b/pkg/route/engine_test.go index acb557826..e522f4f01 100644 --- a/pkg/route/engine_test.go +++ b/pkg/route/engine_test.go @@ -1032,3 +1032,20 @@ func TestAcquireHijackConn(t *testing.T) { assert.DeepEqual(t, engine, hijackConn.e) assert.DeepEqual(t, conn, hijackConn.Conn) } + +func TestMarkAsRunning(t *testing.T) { + // case 1: normal + engine := NewEngine(config.NewOptions(nil)) + engine.Init() + err := engine.MarkAsRunning() + assert.DeepEqual(t, err, nil) + assert.DeepEqual(t, statusRunning, atomic.LoadUint32(&engine.status)) + + // case 2: status is not initialized + engine = NewEngine(config.NewOptions(nil)) + engine.Init() + atomic.StoreUint32(&engine.status, statusRunning) + err = engine.MarkAsRunning() + assert.DeepEqual(t, err, errAlreadyRunning) + assert.DeepEqual(t, statusRunning, atomic.LoadUint32(&engine.status)) +}