github.com/zooyer/miskit@v1.0.71/zuid/zuid_test.go (about) 1 package zuid 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "github.com/zooyer/jsons" 8 "io" 9 "net/http" 10 "net/url" 11 "testing" 12 "time" 13 14 "github.com/bwmarrin/snowflake" 15 ) 16 17 func TestNewNode(t *testing.T) { 18 sf, err := snowflake.NewNode(1) 19 if err != nil { 20 panic(err) 21 } 22 23 yf := Snowflake(snowflake.Epoch, 1) 24 25 sid := sf.Generate() 26 yid := yf.GenID() 27 if sid.Int64() != yid.Int64() { 28 t.Fatal("generate fail", sid, ":", yid) 29 } 30 fmt.Println(sid, ":", yid) 31 32 sid = sf.Generate() 33 yid = yf.GenID() 34 if sid.Int64() != yid.Int64() { 35 t.Fatal("generate fail", sid, ":", yid) 36 } 37 fmt.Println(sid, ":", yid) 38 39 shot := Shotflake(snowflake.Epoch, 1) 40 now := time.Now() 41 for i := 0; i < 16; i++ { 42 fmt.Println(shot.GenID()) 43 } 44 cost := time.Since(now) 45 if seconds := cost.Seconds(); seconds < 2 || seconds > 3 { 46 t.Fatal("generate fail", cost) 47 } 48 fmt.Println(time.Since(now)) 49 50 var id, id2 ID 51 data, err := json.Marshal(id) 52 if err != nil { 53 t.Fatal(err) 54 } 55 if err = json.Unmarshal(data, &id2); err != nil { 56 t.Fatal(err) 57 } 58 if id != id2 { 59 t.Fatal("json marshal error:", id, id2) 60 } 61 } 62 63 func TestTTT(t *testing.T) { 64 var params = url.Values{ 65 "app_id": {"622660"}, 66 "app_secret": {"d566d332f9c78479baba83a6af7f6c45"}, 67 } 68 url, err := url.Parse("http://ice.zhangzhongyuan.online:8004/ice/v1/idgen") 69 if err != nil { 70 t.Fatal(err) 71 } 72 url.RawQuery = params.Encode() 73 const ur = "http://ice.zhangzhongyuan.online:8004/ice/v1/idgen?app_id=622660&app_secret=d566d332f9c78479baba83a6af7f6c45" 74 GenID := func() (id int64, err error) { 75 resp, err := http.Get(url.String()) 76 if err != nil { 77 return 78 } 79 defer resp.Body.Close() 80 81 if resp.StatusCode != http.StatusOK { 82 return 0, errors.New(resp.Status) 83 } 84 85 data, err := io.ReadAll(resp.Body) 86 if err != nil { 87 return 88 } 89 90 val, err := jsons.Unmarshal(data) 91 if err != nil { 92 return 93 } 94 95 if val.Int("errno") != 0 { 96 return 0, errors.New(val.String("errmsg")) 97 } 98 99 return val.Int("data", "id"), nil 100 } 101 102 var now = time.Now() 103 104 for i := 0; i < 16; i++ { 105 id, err := GenID() 106 if err != nil { 107 t.Fatal(err) 108 } 109 fmt.Println(id) 110 } 111 112 t.Log(time.Since(now)) 113 }