73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
// warroom project main.go
|
|
package core
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
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) {
|
|
a := "1:2"
|
|
b := "1"
|
|
|
|
as := strings.SplitN(a, ":", 2)
|
|
bs := strings.SplitN(b, ":", 2)
|
|
|
|
fmt.Println(as, bs)
|
|
}
|