| 1 |
package mq |
| 2 |
|
| 3 |
import "testing" |
| 4 |
|
| 5 |
台创建的Topic |
| 6 |
var Topic = "" |
| 7 |
|
| 8 |
集群URL |
| 9 |
var ENDPOINT = "" |
| 10 |
|
| 11 |
网身份验证访问码 |
| 12 |
var Ak = "" |
| 13 |
|
| 14 |
身份验证密钥 |
| 15 |
var Sk = "" |
| 16 |
|
| 17 |
的Producer ID |
| 18 |
var ProducerID = "" |
| 19 |
|
| 20 |
的Consumer ID |
| 21 |
var ConsumerID = "" |
| 22 |
|
| 23 |
func TestNewClient(t *testing.T) { |
| 24 |
client := NewClient(Ak, Sk, ENDPOINT, Topic, ProducerID, ConsumerID, "http", "http") |
| 25 |
msgId, err := client.Send(GetCurrentUnixMicro(), []byte("hello world")) |
| 26 |
if err != nil { |
| 27 |
t.Error(err) |
| 28 |
} else { |
| 29 |
t.Logf("The message id successfully send is %v", msgId) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func TestReceiveClient(t *testing.T) { |
| 34 |
|
| 35 |
|
| 36 |
client := NewClient(Ak, Sk, ENDPOINT, Topic, ProducerID, ConsumerID, "http", "http") |
| 37 |
respChan := make(chan string) |
| 38 |
errChan := make(chan error) |
| 39 |
end := make(chan int) |
| 40 |
message := "" |
| 41 |
go func() { |
| 42 |
select { |
| 43 |
case resp := <-respChan: |
| 44 |
{ |
| 45 |
t.Logf("message: %s", resp) |
| 46 |
message = resp |
| 47 |
end <- 1 |
| 48 |
} |
| 49 |
case err := <-errChan: |
| 50 |
{ |
| 51 |
t.Logf("err: %v", err) |
| 52 |
end <- 1 |
| 53 |
} |
| 54 |
} |
| 55 |
}() |
| 56 |
|
| 57 |
client.ReceiveMessage(respChan, errChan) |
| 58 |
<-end |
| 59 |
|
| 60 |
if message != "hello world" { |
| 61 |
t.Errorf("message: %s", message) |
| 62 |
} |
| 63 |
} |