114 lines
2.9 KiB
Go
114 lines
2.9 KiB
Go
// warroom project main.go
|
|
package core
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"repositories.action2quare.com/ayo/gocommon"
|
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
|
)
|
|
|
|
func TestPubSub(t *testing.T) {
|
|
opt0, _ := redis.ParseURL("redis://192.168.8.94:6380/0")
|
|
opt1, _ := redis.ParseURL("redis://192.168.8.94:6380/1")
|
|
|
|
rc0 := redis.NewClient(opt0)
|
|
rc1 := redis.NewClient(opt1)
|
|
|
|
go func() {
|
|
time.Sleep(time.Second)
|
|
rc1.Publish(context.Background(), "__testchan", "real???")
|
|
fmt.Println("published")
|
|
}()
|
|
|
|
pubsub := rc0.Subscribe(context.Background(), "__testchan")
|
|
msg, err := pubsub.ReceiveMessage(context.Background())
|
|
fmt.Println(msg.Payload, err)
|
|
}
|
|
func makeHash(chanName string, index uint32) string {
|
|
for len(chanName) < 12 {
|
|
chanName += chanName
|
|
}
|
|
left := chanName[:6]
|
|
right := chanName[len(chanName)-6:]
|
|
base := []byte(left + right)
|
|
for i := 0; i < 12; i++ {
|
|
base[i] += base[12-i-1]
|
|
}
|
|
|
|
bs := make([]byte, 4)
|
|
binary.LittleEndian.PutUint32(bs, index)
|
|
for i, c := range bs {
|
|
base[i] ^= c
|
|
}
|
|
var gid primitive.ObjectID
|
|
copy(gid[:], base)
|
|
|
|
return gid.Hex()
|
|
}
|
|
|
|
func TestNameHash(t *testing.T) {
|
|
for i := 0; i < 10; i++ {
|
|
makeHash("Urud", uint32(i))
|
|
fmt.Printf("Urud:%d - %s\n", i, makeHash("Urud", uint32(i)))
|
|
makeHash("Sheldon", uint32(i))
|
|
fmt.Printf("Sheldon:%d - %s\n", i, makeHash("Sheldon", uint32(i)))
|
|
}
|
|
}
|
|
|
|
func TestReJSON(t *testing.T) {
|
|
rc := redis.NewClient(&redis.Options{Addr: "192.168.8.94:6380"})
|
|
rh := gocommon.NewRedisonHandler(context.Background(), rc)
|
|
|
|
success, err := rc.HSetNX(context.Background(), "setnxtest", "cap", 100).Result()
|
|
fmt.Println(success, err)
|
|
success, err = rc.HSetNX(context.Background(), "setnxtest", "cap", 100).Result()
|
|
fmt.Println(success, err)
|
|
|
|
testDoc := map[string]any{
|
|
"members": map[string]any{
|
|
"mid2": map[string]any{
|
|
"key": "val",
|
|
"exp": 20202020,
|
|
},
|
|
"mid1": map[string]any{
|
|
"key": "val",
|
|
"exp": 10101010,
|
|
},
|
|
},
|
|
}
|
|
|
|
gd := groupDoc{
|
|
id: primitive.NewObjectID(),
|
|
}
|
|
|
|
midin := primitive.NewObjectID()
|
|
tid := gd.tid(midin)
|
|
midout := gd.mid(tid)
|
|
logger.Println(midin, tid, midout)
|
|
|
|
logger.Println(rh.JSONSet("jsontest", "$", testDoc))
|
|
logger.Println(rh.JSONGet("jsontest", "$"))
|
|
logger.Println(rh.JSONResp("jsontest", "$.members"))
|
|
logger.Println(rh.JSONGetString("jsontest", "$.members..key"))
|
|
logger.Println(rh.JSONGetInt64("jsontest", "$.members..exp"))
|
|
logger.Println(rh.JSONObjKeys("jsontest", "$.members"))
|
|
|
|
err = rh.JSONMSet("jsontest", map[string]any{
|
|
"$.members.mid1.key": "newval",
|
|
"$.members.mid2.key": "newval",
|
|
})
|
|
logger.Println(err)
|
|
|
|
logger.Println(rh.JSONGet("jsontest", "$"))
|
|
logger.Println(rh.JSONMDel("jsontest", []string{"$.members.mid1", "$.members.mid2"}))
|
|
logger.Println(rh.JSONGet("jsontest", "$"))
|
|
logger.Println(rh.JSONObjLen("jsontest", "$.members"))
|
|
}
|