Skip to content

Commit

Permalink
Create tester
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 14, 2022
1 parent 078a0e9 commit 7f6d151
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 48 deletions.
22 changes: 9 additions & 13 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{}

Expand All @@ -65,14 +59,16 @@ 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())

return outC, 0, err
}

tester := core.NewTester(watchConfig, outC)

records := strings.Split(config, "\n")
for _, addr := range records {
u, err := url.Parse(addr)
Expand All @@ -81,15 +77,15 @@ 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
}

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")
}
11 changes: 11 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
@@ -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)
}
53 changes: 53 additions & 0 deletions core/tester.go
Original file line number Diff line number Diff line change
@@ -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(), "")
}
35 changes: 0 additions & 35 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package core

import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"time"
)

Expand All @@ -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(), "")
}

0 comments on commit 7f6d151

Please sign in to comment.