libf 1 year ago
parent
commit
3d7c2df896
3 changed files with 75 additions and 0 deletions
  1. 32 0
      context/context_test.go
  2. 19 0
      glob/glob.go
  3. 24 0
      testrun/testrun_test.go

+ 32 - 0
context/context_test.go

@@ -0,0 +1,32 @@
+package context_test
+
+import (
+	"context"
+	"fmt"
+	"testing"
+	"time"
+
+	"git.wecise.com/wecise/common/logger"
+)
+
+func TestContext(t *testing.T) {
+	ctx, cancel := context.WithCancelCause(context.Background())
+	cancel(fmt.Errorf("%s", "cancel"))
+
+	go func() {
+		for {
+			select {
+			case <-ctx.Done():
+				err := ctx.Err()
+				if err == context.Canceled {
+					err = context.Cause(ctx)
+				}
+				logger.Info("context done", err)
+			default:
+			}
+			time.Sleep(5 * time.Second)
+		}
+	}()
+
+	time.Sleep(1 * time.Hour)
+}

+ 19 - 0
glob/glob.go

@@ -0,0 +1,19 @@
+package main
+
+import (
+	"fmt"
+	"io/fs"
+	"path/filepath"
+)
+
+func main() {
+	dir, _ := filepath.Abs("..")
+	// fns, _ := filepath.Glob(dir + "/*/*/*")
+	// fmt.Println(strings.Join(fns, "\n"))
+	// Glob 只能扫描一级目录
+	// Walk 递归所有子目录
+	filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
+		fmt.Println(path)
+		return nil
+	})
+}

+ 24 - 0
testrun/testrun_test.go

@@ -0,0 +1,24 @@
+package testrun_test
+
+import (
+	"testing"
+	"time"
+)
+
+func TestBasic(t *testing.T) {
+
+	t.Run("InitG", func(t *testing.T) {
+		for i := 0; i < 10; i++ {
+			time.Sleep(1 * time.Second)
+			println("InitG", i)
+		}
+	})
+
+	t.Run("Class", func(t *testing.T) {
+		for i := 0; i < 10; i++ {
+			time.Sleep(1 * time.Second)
+			println("Class", i)
+		}
+	})
+
+}