From 8b02b44a34cde0dd36fd1575f96cb100844be138 Mon Sep 17 00:00:00 2001 From: adi Date: Wed, 19 Oct 2022 17:35:20 +0300 Subject: [PATCH] Improved error verification on dsp + tests --- dispatchers/libdispatcher.go | 10 ++-- dispatchers/libdispatcher_test.go | 95 ++++++++++++++++++------------- 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/dispatchers/libdispatcher.go b/dispatchers/libdispatcher.go index d23a1bf5f..036eaacd0 100644 --- a/dispatchers/libdispatcher.go +++ b/dispatchers/libdispatcher.go @@ -229,8 +229,8 @@ func (sd *singleResultDispatcher) Dispatch(dm *engine.DataManager, flts *engine. } if err = callDHwithID(tnt, hostID, routeID, dRh, dm, serviceMethod, args, reply); err == nil || - err != utils.ErrDSPHostNotFound || - !rpcclient.IsNetworkError(err) { // successful dispatch with normal errors + (err != utils.ErrDSPHostNotFound && + !rpcclient.IsNetworkError(err)) { // successful dispatch with normal errors return } if err != nil { @@ -315,7 +315,8 @@ func (ld *loadDispatcher) Dispatch(dm *engine.DataManager, flts *engine.FilterS, serviceMethod, args, reply) lM.decrementLoad(dR.HostID, ld.tntID) // call ended if err == nil || - (err != utils.ErrNotFound && !rpcclient.IsNetworkError(err)) { // successful dispatch with normal errors + (err != utils.ErrDSPHostNotFound && + !rpcclient.IsNetworkError(err)) { // successful dispatch with normal errors return } // not found or network errors will continue with standard dispatching @@ -342,7 +343,8 @@ func (ld *loadDispatcher) Dispatch(dm *engine.DataManager, flts *engine.FilterS, serviceMethod, args, reply) lM.decrementLoad(hostID, ld.tntID) // call ended if err == nil || - (err != utils.ErrNotFound && !rpcclient.IsNetworkError(err)) { // successful dispatch with normal errors + (err != utils.ErrDSPHostNotFound && + !rpcclient.IsNetworkError(err)) { // successful dispatch with normal errors return } if err != nil { diff --git a/dispatchers/libdispatcher_test.go b/dispatchers/libdispatcher_test.go index 1c43d152b..572b9b0ae 100644 --- a/dispatchers/libdispatcher_test.go +++ b/dispatchers/libdispatcher_test.go @@ -456,7 +456,7 @@ func TestLibDispatcherLoadDispatcherDispatchHostsID(t *testing.T) { } } -func TestLibDispatcherLoadStrategyDispatchCaseCallError(t *testing.T) { +func TestLibDispatcherLoadStrategyDispatchCaseCallError2(t *testing.T) { wgDsp := &loadDispatcher{ hosts: engine.DispatcherHostProfiles{ { @@ -661,7 +661,7 @@ func TestLibDispatcherLoadDispatcherCacheError(t *testing.T) { engine.Cache = cacheInit } -func TestLibDispatcherLoadDispatcherCacheError4(t *testing.T) { +func TestLibDispatcherLoadDispatcherCacheError7(t *testing.T) { cacheInit := engine.Cache cfg := config.NewDefaultCGRConfig() cfg.CacheCfg().ReplicationConns = []string{"con"} @@ -911,42 +911,6 @@ func TestLibDispatcherSingleResultDispatcherCase3(t *testing.T) { engine.IntRPC = tmp } -func TestLibDispatcherDispatchFilterError(t *testing.T) { - cfg := config.NewDefaultCGRConfig() - flts := engine.NewFilterS(cfg, nil, nil) - var dsp Dispatcher = &singleResultDispatcher{ - sorter: new(noSort), - hosts: engine.DispatcherHostProfiles{{ - ID: "testID", - FilterIDs: []string{"*wrongType"}, - }}, - } - expErrMsg := "inline parse error for string: <*wrongType>" - if err := dsp.Dispatch(nil, flts, nil, "", "", &DispatcherRoute{}, "", "", ""); err == nil || err.Error() != expErrMsg { - t.Errorf("Expected error: %s received: %v", expErrMsg, err) - } - dsp = &loadDispatcher{ - sorter: new(noSort), - hosts: engine.DispatcherHostProfiles{{ - ID: "testID2", - FilterIDs: []string{"*wrongType"}, - }}, - defaultRatio: 1, - } - if err := dsp.Dispatch(nil, flts, nil, "", "", &DispatcherRoute{}, "", "", ""); err == nil || err.Error() != expErrMsg { - t.Errorf("Expected error: %s received: %v", expErrMsg, err) - } - dsp = &broadcastDispatcher{ - hosts: engine.DispatcherHostProfiles{{ - ID: "testID", - FilterIDs: []string{"*wrongType"}, - }}, - } - if err := dsp.Dispatch(nil, flts, nil, "", "", &DispatcherRoute{}, "", "", ""); err == nil || err.Error() != expErrMsg { - t.Errorf("Expected error: %s received: %v", expErrMsg, err) - } -} - func TestLibDispatcherRandomSort(t *testing.T) { cfg := config.NewDefaultCGRConfig() flts := engine.NewFilterS(cfg, nil, nil) @@ -988,3 +952,58 @@ func TestLibDispatcherRoundRobinSort(t *testing.T) { t.Errorf("Expected: %q, received: %q", expHostIDs2, hostIDs) } } + +func TestLibDispatcherLoadStrategyDispatchCaseCallError(t *testing.T) { + wgDsp := &loadDispatcher{ + hosts: engine.DispatcherHostProfiles{ + { + ID: "hostID", + }, + }, + defaultRatio: 1, + sorter: new(noSort), + } + err := wgDsp.Dispatch(nil, nil, nil, "cgrates.org", "", &DispatcherRoute{}, utils.AttributeSv1Ping, &utils.CGREvent{}, &wgDsp) + expected := "NO_DATABASE_CONNECTION" + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, err) + } +} + +func TestLibDispatcherDispatchFilterError(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + flts := engine.NewFilterS(cfg, nil, nil) + data := engine.NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := engine.NewDataManager(data, cfg.CacheCfg(), nil) + var dsp Dispatcher = &singleResultDispatcher{ + sorter: new(noSort), + hosts: engine.DispatcherHostProfiles{{ + ID: "testID", + FilterIDs: []string{"*wrongType"}, + }}, + } + expErrMsg := "inline parse error for string: <*wrongType>" + if err := dsp.Dispatch(dm, flts, nil, "cgrates.org", "", &DispatcherRoute{}, "", "", ""); err == nil || err.Error() != expErrMsg { + t.Errorf("Expected error: %s received: %v", expErrMsg, err) + } + dsp = &loadDispatcher{ + sorter: new(noSort), + hosts: engine.DispatcherHostProfiles{{ + ID: "testID2", + FilterIDs: []string{"*wrongType"}, + }}, + defaultRatio: 1, + } + if err := dsp.Dispatch(dm, flts, nil, "cgrates.org", "", &DispatcherRoute{}, "", "", ""); err == nil || err.Error() != expErrMsg { + t.Errorf("Expected error: %s received: %v", expErrMsg, err) + } + dsp = &broadcastDispatcher{ + hosts: engine.DispatcherHostProfiles{{ + ID: "testID", + FilterIDs: []string{"*wrongType"}, + }}, + } + if err := dsp.Dispatch(dm, flts, nil, "cgrates.org", "", &DispatcherRoute{}, "", "", ""); err == nil || err.Error() != expErrMsg { + t.Errorf("Expected error: %s received: %v", expErrMsg, err) + } +}