From 58b864507c47a0876ca87da0c6fc8833c35a07b5 Mon Sep 17 00:00:00 2001 From: Bruce Date: Mon, 8 Oct 2018 21:08:58 +0800 Subject: [PATCH] test factory --- playground/basic/address_test.go | 6 +-- playground/cron/cron_test.go | 86 ++++++++++++++++++++++++++++++++ playground/cron/iso8601parser.go | 35 +++++++++++++ playground/factory/robot.go | 14 ++++++ playground/factory/robot_test.go | 8 +++ playground/factory/robotfuncs.go | 16 ++++++ 6 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 playground/cron/cron_test.go create mode 100644 playground/cron/iso8601parser.go create mode 100644 playground/factory/robot.go create mode 100644 playground/factory/robot_test.go create mode 100644 playground/factory/robotfuncs.go diff --git a/playground/basic/address_test.go b/playground/basic/address_test.go index 435c9d9..dec36ec 100644 --- a/playground/basic/address_test.go +++ b/playground/basic/address_test.go @@ -31,10 +31,10 @@ func TestStructAddress(t *testing.T) { } func passStructByVariable(a robot) { - fmt.Printf("%T, %v, %p \n", a, a, &a) + fmt.Printf("[passStructByVariable] %T, %v, %p \n", a, a, &a) } func passStructByPointer(a *robot) { - fmt.Printf("%T, %v, %p \n", a, a, &a) - fmt.Printf("%T, %v, %p \n", *a, *a, &*a) + fmt.Printf("[passStructByPointer] %T, %v, %p \n", a, a, &a) + fmt.Printf("[passStructByPointer] %T, %v, %p \n", *a, *a, &*a) } diff --git a/playground/cron/cron_test.go b/playground/cron/cron_test.go new file mode 100644 index 0000000..6daa726 --- /dev/null +++ b/playground/cron/cron_test.go @@ -0,0 +1,86 @@ +package cron + +import ( + "fmt" + "testing" + "time" + + "github.com/edgexfoundry/edgex-go/pkg/models" + "github.com/robfig/cron" +) + +type Job struct { + schedule models.Schedule + scheduleEvents []models.ScheduleEvent +} + +func (job Job) Run() { + fmt.Println(job.schedule.Name, job.schedule.Frequency) +} + +func TestCronWithAddJob(t *testing.T) { + var job = Job{ + schedule: models.Schedule{ + Id: "xxx", + Name: "5sec-schedule", + Frequency: "PT5S", + }, + scheduleEvents: []models.ScheduleEvent{}, + } + var job2 = Job{ + schedule: models.Schedule{ + Id: "xxx", + Name: "2sec-schedule", + Frequency: "PT2S", + }, + scheduleEvents: []models.ScheduleEvent{}, + } + + // init cron + c := cron.New() + + // add cron job + var spec = fmt.Sprintf("@every %v", ParseDuration(job.schedule.Frequency)) + c.AddJob(spec, job) + + spec = fmt.Sprintf("@every %v", ParseDuration(job2.schedule.Frequency)) + c.AddJob(spec, job2) + + // start cron + c.Start() + + time.Sleep(10 * time.Second) + // keep alive + //select {} +} + +func TestCronWithAddFunc(t *testing.T) { + // init cron + c := cron.New() + + // add cron job + var duration = ParseDuration("PT2S") + var spec = fmt.Sprintf("@every %v", duration) + + c.AddFunc(spec, func() { + // @every 2s + fmt.Println(spec) + }) + + // start cron + c.Start() + + // keep alive + select {} +} + +func TestParseISO8601(t *testing.T) { + var duration = ParseDuration("PT2S") + + // PT2S -> 2s + fmt.Println(duration) + // PT15M -> 15m0s + fmt.Println(ParseDuration("PT15M")) + // P12Y4MT15M -> 108000h15m0s + fmt.Println(ParseDuration("P12Y4MT15M")) +} diff --git a/playground/cron/iso8601parser.go b/playground/cron/iso8601parser.go new file mode 100644 index 0000000..509d87c --- /dev/null +++ b/playground/cron/iso8601parser.go @@ -0,0 +1,35 @@ +package cron + +import ( + "regexp" + "strconv" + "time" +) + +func ParseDuration(str string) time.Duration { + durationRegex := regexp.MustCompile(`P(?P\d+Y)?(?P\d+M)?(?P\d+D)?T?(?P\d+H)?(?P\d+M)?(?P\d+S)?`) + matches := durationRegex.FindStringSubmatch(str) + + years := ParseInt64(matches[1]) + months := ParseInt64(matches[2]) + days := ParseInt64(matches[3]) + hours := ParseInt64(matches[4]) + minutes := ParseInt64(matches[5]) + seconds := ParseInt64(matches[6]) + + hour := int64(time.Hour) + minute := int64(time.Minute) + second := int64(time.Second) + return time.Duration(years*24*365*hour + months*30*24*hour + days*24*hour + hours*hour + minutes*minute + seconds*second) +} + +func ParseInt64(value string) int64 { + if len(value) == 0 { + return 0 + } + parsed, err := strconv.Atoi(value[:len(value)-1]) + if err != nil { + return 0 + } + return int64(parsed) +} diff --git a/playground/factory/robot.go b/playground/factory/robot.go new file mode 100644 index 0000000..9f6944f --- /dev/null +++ b/playground/factory/robot.go @@ -0,0 +1,14 @@ +package factory + +import "log" + +var speakFuncs = make(map[string]interface{}) + +func say(funcName string) { + speakFunc, ok := speakFuncs[funcName] + if !ok { + log.Println("speakFunc not exist") + } else { + speakFunc.(func())() + } +} diff --git a/playground/factory/robot_test.go b/playground/factory/robot_test.go new file mode 100644 index 0000000..c8355ba --- /dev/null +++ b/playground/factory/robot_test.go @@ -0,0 +1,8 @@ +package factory + +import "testing" + +func TestSay(t *testing.T) { + say("xxx") + say("hi") +} diff --git a/playground/factory/robotfuncs.go b/playground/factory/robotfuncs.go new file mode 100644 index 0000000..cb68aed --- /dev/null +++ b/playground/factory/robotfuncs.go @@ -0,0 +1,16 @@ +package factory + +import "log" + +func sayHi() { + log.Print("Hi~") +} + +func sayHello() { + log.Print("Hello~") +} + +func init() { + speakFuncs["hi"] = sayHi + speakFuncs["hello"] = sayHello +}