test factory

This commit is contained in:
Bruce 2018-10-08 21:08:58 +08:00
parent f53452e06d
commit 58b864507c
No known key found for this signature in database
GPG key ID: C715526B381CAF28
6 changed files with 162 additions and 3 deletions

View file

@ -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)
}

View file

@ -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"))
}

View file

@ -0,0 +1,35 @@
package cron
import (
"regexp"
"strconv"
"time"
)
func ParseDuration(str string) time.Duration {
durationRegex := regexp.MustCompile(`P(?P<years>\d+Y)?(?P<months>\d+M)?(?P<days>\d+D)?T?(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\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)
}

View file

@ -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())()
}
}

View file

@ -0,0 +1,8 @@
package factory
import "testing"
func TestSay(t *testing.T) {
say("xxx")
say("hi")
}

View file

@ -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
}