package remind import ( "b612.me/stardb" "fmt" _ "github.com/glebarez/go-sqlite" "os" "testing" "time" ) func exists(path string) bool { _, err := os.Stat(path) if os.IsNotExist(err) { return false } return err == nil } func TestStarDB(t *testing.T) { var db stardb.StarDB err := db.Open("sqlite", "./db.sqlite") if err != nil { t.Fatal(err) } var r *Remind r, err = NewRemind(&db, func(task Task) { fmt.Println("hello world!", time.Now(), task.ID, task.Key, task.TmrInfo, task.Msg, task.Origin) fmt.Println(task.GetTimer().NextTimer(), task.GetTimer().IsRunning()) fmt.Println(r.ListTasks()) fmt.Println("ok") time.Sleep(time.Second * 10) fmt.Println(task.GetTimer().NextTimer()) }) tk, err := r.AddTask("每天下午3点52分20秒提醒我吃饭", "00", []byte{}) if err != nil { t.Fatal(err) } fmt.Println(tk.GetTimer().NextTimer()) fmt.Println(r.ListTasks()) fmt.Println("hello") time.Sleep(time.Second * 120) } func TestOtherDb(t *testing.T) { var db stardb.StarDB err := db.Open("sqlite", "./db.sqlite") if err != nil { t.Fatal(err) } r, err := NewRemind(&db, func(task Task) { fmt.Println("hello world!", time.Now(), task.ID, task.Key, task.TmrInfo, task.Msg, task.Origin) }) if err != nil { t.Fatal(err) } fmt.Println(len(r.GetTasksByKey("705457681"))) } func TestRemind(t *testing.T) { var db stardb.StarDB err := db.Open("sqlite", "./remind.db") if err != nil { t.Fatal(err) } sign := make(chan int) r, err := NewRemind(&db, func(task Task) { fmt.Println("hello world!", time.Now(), task.ID, task.Key, task.TmrInfo, task.Msg, task.Origin) sign <- 1 }) if err != nil { t.Fatal(err) } fmt.Println("length of remind", len(r.ListTasks())) if len(r.ListTasks()) < 2 { tk, err := r.AddTask("每10秒提醒我吃饭", "干饭", []byte{1, 2, 3}) if err != nil { t.Fatal(err) } tmr := tk.GetTimer() fmt.Println(tmr.NextTimer()) } i := 0 for { select { case <-sign: i++ break case <-time.After(time.Second * 15): t.FailNow() } time.Sleep(time.Second) fmt.Println(len(r.ListTasks())) if i == 3 { break } } }