-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Ccheers/dev
Dev
- Loading branch information
Showing
10 changed files
with
184 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
module github.com/ccheers/xpkg | ||
|
||
go 1.21 | ||
|
||
toolchain go1.21.4 | ||
go 1.22 | ||
|
||
require ( | ||
github.com/BurntSushi/toml v0.4.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"crypto/md5" | ||
"encoding/hex" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
func TestMsgBus_Pop(t *testing.T) { | ||
ctx := context.TODO() | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: "127.0.0.1:6379", | ||
DB: 0, | ||
}) | ||
msg := []byte("test") | ||
type fields struct { | ||
client *redis.Client | ||
} | ||
type args struct { | ||
ctx context.Context | ||
topic string | ||
channel string | ||
blockTimeout time.Duration | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want []byte | ||
want1 func() | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "1", | ||
fields: fields{ | ||
client: client, | ||
}, | ||
args: args{ | ||
ctx: ctx, | ||
topic: "test", | ||
channel: "test2", | ||
blockTimeout: 0, | ||
}, | ||
want: msg, | ||
want1: nil, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
x := &MsgBus{ | ||
client: tt.fields.client, | ||
} | ||
_ = x.AddChannel(ctx, tt.args.topic, tt.args.channel) | ||
_ = x.Push(ctx, tt.args.topic, msg) | ||
got, ack, err := x.Pop(tt.args.ctx, tt.args.topic, tt.args.channel, tt.args.blockTimeout) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Pop() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Pop() got = %v, want %v", got, tt.want) | ||
} | ||
// 如果没有 ack 则 有数据在 | ||
|
||
md5Bs := md5.Sum(msg) | ||
ackKey := msgBusAckKey(time.Now(), hex.EncodeToString(md5Bs[:])) | ||
bs := x.client.Get(ctx, ackKey).Val() | ||
t.Logf("ack key: %s, ack value: %s", ackKey, bs) | ||
ack() | ||
if !errors.Is(x.client.Get(ctx, ackKey).Err(), redis.Nil) { | ||
t.Errorf("ack failed, ack key: %s", ackKey) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters