Skip to content

Commit

Permalink
新增一些测试
Browse files Browse the repository at this point in the history
测试了go-redis 实现解析和转化方法,替代在拿数据的时候进行手动的解析
测试net包发送post请求功能
  • Loading branch information
peifengll committed Mar 25, 2024
1 parent 83a6cfe commit 978f9f9
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/google/martian v2.1.0+incompatible
github.com/google/uuid v1.4.0
github.com/gorilla/websocket v1.5.1
github.com/prometheus/client_golang v1.17.0
github.com/redis/go-redis/v9 v9.5.1
github.com/spf13/viper v1.17.0
github.com/urfave/cli/v2 v2.27.1
github.com/zeromicro/go-zero v1.6.0
Expand Down Expand Up @@ -45,7 +47,6 @@ require (
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ github.com/alicebob/gopher-json v0.0.0-20230218143504-906a9b012302 h1:uvdUDbHQHO
github.com/alicebob/miniredis/v2 v2.31.0 h1:ObEFUNlJwoIiyjxdrYF0QIDE7qXcLc7D3WpSH4c22PU=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
Expand Down Expand Up @@ -367,6 +369,8 @@ github.com/prometheus/common v0.44.0 h1:+5BrQJwiBB9xsMygAB3TNvpQKOwlkc25LbISbrdO
github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO7x0VV9VvuY=
github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwaUuI=
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
Expand Down
58 changes: 58 additions & 0 deletions test_goredis/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"context"
"encoding/json"
"fmt"
"github.com/redis/go-redis/v9"
"log"
)

type RedisObj struct {
*redis.Client
}

func NewRedis() RedisObj {
client := redis.NewClient(&redis.Options{
Addr: "peifeng.site:6379", // redis地址
Password: "ningzaichun", // 密码
DB: 0, // 使用默认数据库
})
return RedisObj{client}
}

type AutoGenerated struct {
DpvMatchCode string `json:"dpv_match_code"`
DpvFootnotes string `json:"dpv_footnotes"`
DpvCmra string `json:"dpv_cmra"`
DpvVacant string `json:"dpv_vacant"`
Active string `json:"active"`
}

func (m *AutoGenerated) MarshalBinary() (data []byte, err error) {
return json.Marshal(m)
}

func (m *AutoGenerated) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, m)

}

func main() {
r := NewRedis()
ctx := context.Background()
a := &AutoGenerated{
DpvMatchCode: "Y",
DpvFootnotes: "AABB",
DpvCmra: "N",
DpvVacant: "N",
Active: "Y",
}

result, err := r.RPush(ctx, "666", a).Result()
if err != nil {
log.Fatal(err)
}
fmt.Println(result)

}
4 changes: 4 additions & 0 deletions test_goredis/reademe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# lpush

这个功能用的时候要注意不能直接放结构体变量进去,
一般来说还是自己做解析,(记得可以自己为其实现 解析和读取的方法,然后就可以放了)
22 changes: 22 additions & 0 deletions testany/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

type name struct {
}

func main() {
var i interface{}
// 检查 interface 变量是否为 nil
if i == nil {
fmt.Println("i 是 nil")
} else {
fmt.Println("i 不是 nil")
}
i = nil
if i == nil {
fmt.Println("i 是 nil")
} else {
fmt.Println("i 不是 nil")
}
}
21 changes: 21 additions & 0 deletions testjson/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"encoding/json"
"fmt"
)

type Corp struct {
ID int32 `json:"id" gorm:"Column:id;type:int;not null;primaryKey:KEY"`
Cid string `json:"cid,omitempty" gorm:"Column:cid;type:varchar(32);not null;default:'';uniqueIndex:KEY"` // Cid 集团用户注册登录时的用户名

}

func main() {
var p Corp
marshal, err := json.Marshal(p)
if err != nil {
return
}
fmt.Println(string(marshal))
}
21 changes: 21 additions & 0 deletions testnet/req/api/address.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
INSERT INTO address (uname, phone, address, u_id) VALUES
('张三', '13812345678', '北京市朝阳区', 85),
('李四', '13912345678', '上海市浦东新区', 85),
('王五', '13612345678', '广州市天河区', 85),
('赵六', '13712345678', '深圳市南山区', 85),
('钱七', '13512345678', '成都市武侯区', 85),
('孙八', '13412345678', '杭州市西湖区', 85),
('周九', '13312345678', '南京市玄武区', 85),
('吴十', '13212345678', '武汉市江岸区', 85),
('郑十一', '13112345678', '重庆市渝中区', 85),
('王十二', '13012345678', '长沙市岳麓区', 85),
('冯十三', '13912345679', '西安市雁塔区', 85),
('陈十四', '13912345670', '郑州市金水区', 85),
('楚十五', '13912345671', '苏州市姑苏区', 85),
('魏十六', '13912345672', '天津市和平区', 85),
('蔡十七', '13912345673', '青岛市市北区', 85),
('曹十八', '13912345674', '大连市西岗区', 85),
('庞十九', '13912345675', '沈阳市和平区', 85),
('严二十', '13912345676', '长春市南关区', 85),
('丁二十一', '13912345677', '哈尔滨市道里区', 85),
('贾二十二', '13912345680', '南昌市东湖区', 85);
3 changes: 3 additions & 0 deletions testnet/req/api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package api

var BaseUrl = `http://peifeng.site:8000/`
37 changes: 37 additions & 0 deletions testnet/req/api/order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)

type Order struct {
*http.Client
}

func (o *Order) OrderAdd(data map[string]any) {
url := BaseUrl + "order/add"
// 将数据编码为 JSON 格式
jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println("Error encoding JSON:", err)
return
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := o.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}

defer resp.Body.Close()
// 打印响应内容
fmt.Println("Response:", resp.Status)
}
18 changes: 18 additions & 0 deletions testnet/req/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/peifengll/tests/testnet/req/api"
"net/http"
)

func main() {
client := &http.Client{}
o := api.Order{client}
redata := map[string]any{
"user_id": 85,
"money": 78,
"cart_id": "[43]",
"address_id": 3,
}
o.OrderAdd(redata)
}
17 changes: 17 additions & 0 deletions testnet/req/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"math/rand"
"testing"
"time"
)

func TestRandom(t *testing.T) {
// 设置种子
rand.Seed(time.Now().UnixNano())

// 生成随机数
randomNum := rand.Intn(30) // 生成 0 到 100 之间的随机整数
fmt.Println("随机数:", randomNum)
}
41 changes: 41 additions & 0 deletions testsocket/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"bufio"
"fmt"
"net"
"os"
"strings"
)

func main() {
// 1、与服务端建立连接
conn, err := net.Dial("tcp", "127.0.0.1:9090")
if err != nil {
fmt.Printf("conn server failed, err:%v\n", err)
return
}
// 2、使用 conn 连接进行数据的发送和接收
input := bufio.NewReader(os.Stdin)
for {
s, _ := input.ReadString('\n')
s = strings.TrimSpace(s)
if strings.ToUpper(s) == "Q" {
return
}

_, err = conn.Write([]byte(s))
if err != nil {
fmt.Printf("send failed, err:%v\n", err)
return
}
// 从服务端接收回复消息
var buf [1024]byte
n, err := conn.Read(buf[:])
if err != nil {
fmt.Printf("read failed:%v\n", err)
return
}
fmt.Printf("收到服务端回复:%v\n", string(buf[:n]))
}
}
53 changes: 53 additions & 0 deletions testsocket/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"bufio"
"fmt"
"net"
)

func process(conn net.Conn) {
// 处理完关闭连接
defer conn.Close()

// 针对当前连接做发送和接受操作
for {
reader := bufio.NewReader(conn)
var buf [128]byte
n, err := reader.Read(buf[:])
if err != nil {
fmt.Printf("read from conn failed, err:%v\n", err)
break
}

recv := string(buf[:n])
fmt.Printf("收到的数据:%v\n", recv)

// 将接受到的数据返回给客户端
_, err = conn.Write([]byte("ok"))
if err != nil {
fmt.Printf("write from conn failed, err:%v\n", err)
break
}
}
}

func main() {
// 建立 tcp 服务
listen, err := net.Listen("tcp", "127.0.0.1:9090")
if err != nil {
fmt.Printf("listen failed, err:%v\n", err)
return
}

for {
// 等待客户端建立连接
conn, err := listen.Accept()
if err != nil {
fmt.Printf("accept failed, err:%v\n", err)
continue
}
// 启动一个单独的 goroutine 去处理连接
go process(conn)
}
}

0 comments on commit 978f9f9

Please sign in to comment.