From 7f6d151097f5270ab547f7948cafdc14478ee4a8 Mon Sep 17 00:00:00 2001 From: Denys Kashkovskyi Date: Thu, 14 Apr 2022 09:06:29 +0000 Subject: [PATCH] Create tester --- cmd/watch.go | 22 +++++++++------------ core/config.go | 11 +++++++++++ core/tester.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ core/utils.go | 35 --------------------------------- 4 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 core/config.go create mode 100644 core/tester.go diff --git a/cmd/watch.go b/cmd/watch.go index 010af9e..85c20c6 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -31,13 +31,7 @@ import ( "github.com/spf13/cobra" ) -type WatchConfig struct { - configUrl string - testInterval int - requestTimeout int -} - -var watchConfig = WatchConfig{} +var watchConfig = core.WatchConfig{} // watchCmd represents the watch command var watchCmd = &cobra.Command{ @@ -47,7 +41,7 @@ var watchCmd = &cobra.Command{ } func runWatch(cmd *cobra.Command, args []string) { - log.Default().Println("Testing URLs from config ", watchConfig.configUrl) + log.Default().Println("Testing URLs from config ", watchConfig.ConfigUrl) printer := core.NewPrinter() res := sync.Map{} @@ -65,7 +59,7 @@ func runWatch(cmd *cobra.Command, args []string) { } func doWatch() (chan core.TestResult, int, error) { - config, err := core.GetStringFromURL(watchConfig.configUrl) + config, err := watchConfig.Update() outC := make(chan core.TestResult, 50) if err != nil { log.Fatalf("Could not obtain a config: %v", err.Error()) @@ -73,6 +67,8 @@ func doWatch() (chan core.TestResult, int, error) { return outC, 0, err } + tester := core.NewTester(watchConfig, outC) + records := strings.Split(config, "\n") for _, addr := range records { u, err := url.Parse(addr) @@ -81,7 +77,7 @@ func doWatch() (chan core.TestResult, int, error) { continue } - go core.Test(u, watchConfig.requestTimeout, watchConfig.testInterval, outC) + go tester.Test(u) } return outC, len(records), nil @@ -89,7 +85,7 @@ func doWatch() (chan core.TestResult, int, error) { func init() { rootCmd.AddCommand(watchCmd) - watchCmd.Flags().StringVarP(&watchConfig.configUrl, "configUrl", "c", core.ITArmyConfigURL, "Url of config containing url list") - watchCmd.Flags().IntVarP(&watchConfig.testInterval, "testInterval", "i", 10, "Interval in seconds between test updates") - watchCmd.Flags().IntVarP(&watchConfig.requestTimeout, "requestTimeout", "t", 5, "Request timeout") + watchCmd.Flags().StringVarP(&watchConfig.ConfigUrl, "configUrl", "c", core.ITArmyConfigURL, "Url of config containing url list") + watchCmd.Flags().IntVarP(&watchConfig.TestInterval, "testInterval", "i", 10, "Interval in seconds between test updates") + watchCmd.Flags().IntVarP(&watchConfig.RequestTimeout, "requestTimeout", "t", 5, "Request timeout") } diff --git a/core/config.go b/core/config.go new file mode 100644 index 0000000..4325ac3 --- /dev/null +++ b/core/config.go @@ -0,0 +1,11 @@ +package core + +type WatchConfig struct { + ConfigUrl string + TestInterval int + RequestTimeout int +} + +func (c *WatchConfig) Update() (string, error) { + return GetStringFromURL(c.ConfigUrl) +} diff --git a/core/tester.go b/core/tester.go new file mode 100644 index 0000000..9af559a --- /dev/null +++ b/core/tester.go @@ -0,0 +1,53 @@ +package core + +import ( + "fmt" + "net/url" + "regexp" + "time" +) + +type TestResult struct { + Id string + url url.URL + status string + duration time.Duration +} + +type Tester struct { + requestTimeout time.Duration + testInterval time.Duration + out chan TestResult +} + +func NewTester(config WatchConfig, out chan TestResult) Tester { + return Tester { + requestTimeout: time.Duration(config.RequestTimeout) * time.Second, + testInterval: time.Duration(config.TestInterval) * time.Second, + out: out, + } +} + +func (t *Tester)Test(url *url.URL) { + tp := NewTransport(t.requestTimeout) + + for { + t.out <- TestResult{Id: url.Host, url: *url, status: "Test"} + + _, err := tp.Dial(url.Scheme, url.Host) + + if err != nil { + t.out <- TestResult{Id: url.Host, url: *url, status: formatError(err, url), duration: tp.ConnDuration()} + return + } + + t.out <- TestResult{Id: url.Host, url: *url, status: "OK", duration: tp.Duration()} + time.Sleep(t.testInterval) + + } +} + +func formatError(err error, url *url.URL) string { + m := regexp.MustCompile(fmt.Sprintf(`(net/http: )| \(.*\)|(dial .* %s: )`, url.Host)) + return m.ReplaceAllString(err.Error(), "") +} diff --git a/core/utils.go b/core/utils.go index 3a9f935..2f1c8ea 100644 --- a/core/utils.go +++ b/core/utils.go @@ -2,11 +2,8 @@ package core import ( "context" - "fmt" "io" "net/http" - "net/url" - "regexp" "time" ) @@ -33,35 +30,3 @@ func GetStringFromURL(url string) (string, error) { return string(buf), nil } - -type TestResult struct { - Id string - url url.URL - status string - duration time.Duration -} - -func Test(url *url.URL, timeoutSeconds int, testInterval int, out chan TestResult) { - timeout := time.Duration(timeoutSeconds) * time.Second - tp := NewTransport(timeout) - - for { - out <- TestResult{Id: url.Host, url: *url, status: "Test"} - - _, err := tp.Dial(url.Scheme, url.Host) - - if err != nil { - out <- TestResult{Id: url.Host, url: *url, status: formatError(err, url), duration: tp.ConnDuration()} - return - } - - out <- TestResult{Id: url.Host, url: *url, status: "OK", duration: tp.Duration()} - time.Sleep(time.Duration(testInterval) * time.Second) - - } -} - -func formatError(err error, url *url.URL) string { - m := regexp.MustCompile(fmt.Sprintf(`(net/http: )| \(.*\)|(dial .* %s: )`, url.Host)) - return m.ReplaceAllString(err.Error(), "") -}