Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
package syncUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRWNewLocker1(t *testing.T) {
|
||||
count := 1000000
|
||||
succeedCount := 0
|
||||
expected := 1000000
|
||||
goroutineCount := 1
|
||||
|
||||
lockerObj := NewRWLocker()
|
||||
ch := make(chan bool, goroutineCount)
|
||||
|
||||
for i := 0; i < goroutineCount; i++ {
|
||||
go rwLockerTest(lockerObj, &succeedCount, count/goroutineCount, ch)
|
||||
}
|
||||
|
||||
for i := 0; i < goroutineCount; i++ {
|
||||
<-ch
|
||||
}
|
||||
|
||||
if succeedCount != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, succeedCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRWNewLocker2(t *testing.T) {
|
||||
count := 1000000
|
||||
succeedCount := 0
|
||||
expected := 1000000
|
||||
goroutineCount := 100
|
||||
|
||||
lockerObj := NewRWLocker()
|
||||
ch := make(chan bool, goroutineCount)
|
||||
|
||||
for i := 0; i < goroutineCount; i++ {
|
||||
go rwLockerTest(lockerObj, &succeedCount, count/goroutineCount, ch)
|
||||
}
|
||||
|
||||
for i := 0; i < goroutineCount; i++ {
|
||||
<-ch
|
||||
}
|
||||
|
||||
if succeedCount != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, succeedCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRWNewLocker3(t *testing.T) {
|
||||
count := 1000000
|
||||
succeedCount := 0
|
||||
expected := 1000000
|
||||
goroutineCount := 10000
|
||||
|
||||
lockerObj := NewRWLocker()
|
||||
ch := make(chan bool, goroutineCount)
|
||||
|
||||
for i := 0; i < goroutineCount; i++ {
|
||||
go rwLockerTest(lockerObj, &succeedCount, count/goroutineCount, ch)
|
||||
}
|
||||
|
||||
for i := 0; i < goroutineCount; i++ {
|
||||
<-ch
|
||||
}
|
||||
|
||||
if succeedCount != expected {
|
||||
t.Errorf("Expected %d, but got %d", expected, succeedCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRWNewLocker4(t *testing.T) {
|
||||
lockerObj := NewRWLocker()
|
||||
if successful, _, _ := lockerObj.RLock(100); successful == false {
|
||||
t.Errorf("It should be successful to get a read lock, but now it fails.")
|
||||
return
|
||||
}
|
||||
if successful, _, _ := lockerObj.RLock(100); successful == false {
|
||||
t.Errorf("It should be successful to get a read lock, but now it fails.")
|
||||
return
|
||||
}
|
||||
if successful, _, _ := lockerObj.RLock(100); successful == false {
|
||||
t.Errorf("It should be successful to get a read lock, but now it fails.")
|
||||
return
|
||||
}
|
||||
lockerObj.RUnlock()
|
||||
lockerObj.RUnlock()
|
||||
lockerObj.RUnlock()
|
||||
|
||||
if successful, _, _ := lockerObj.Lock(100); successful == false {
|
||||
t.Errorf("It should be successful to get a write lock, but now it fails.")
|
||||
return
|
||||
}
|
||||
if successful, _, _ := lockerObj.Lock(100); successful {
|
||||
t.Errorf("It should be failed to get a write lock, but now it succeeds.")
|
||||
return
|
||||
}
|
||||
if successful, _, _ := lockerObj.RLock(100); successful {
|
||||
t.Errorf("It should be failed to get a read lock, but now it succeeds.")
|
||||
return
|
||||
}
|
||||
|
||||
lockerObj.Unlock()
|
||||
}
|
||||
|
||||
func TestRWNewLocker5(t *testing.T) {
|
||||
count := 100
|
||||
rwLockerObj := NewRWLocker()
|
||||
ch := make(chan bool, 100)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
go func(num int, ch chan bool) {
|
||||
if num%2 == 0 {
|
||||
if successful, _, _ := rwLockerObj.Lock(100); successful {
|
||||
fmt.Println("I get write lock.")
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
rwLockerObj.Unlock()
|
||||
} else {
|
||||
fmt.Println("Write lock timeout")
|
||||
}
|
||||
} else {
|
||||
if successful, _, _ := rwLockerObj.RLock(100); successful {
|
||||
fmt.Println("I get read lock.")
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
rwLockerObj.RUnlock()
|
||||
} else {
|
||||
fmt.Println("Read lock timeout")
|
||||
}
|
||||
}
|
||||
ch <- true
|
||||
}(i, ch)
|
||||
}
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
<-ch
|
||||
}
|
||||
}
|
||||
|
||||
func rwLockerTest(lockerObj *RWLocker, succeedCount *int, count int, ch chan bool) {
|
||||
if success, _, _ := lockerObj.Lock(10000); !success {
|
||||
fmt.Printf("[%v]获取锁超时\n", time.Now())
|
||||
return
|
||||
}
|
||||
defer lockerObj.Unlock()
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
*succeedCount += 1
|
||||
}
|
||||
|
||||
ch <- true
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package mqMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
queueObj = NewQueue(MQ_REGION_GUANGZHOU, "AD-Click", "AKIDjBUWGmIkDPJHCqRb2DCQM2RbUUL1MWMx", "RcldV6PRCBwUkjGVdVjiPq0IJ2VhEZtO")
|
||||
)
|
||||
|
||||
func TestHandleDelaySeconds(t *testing.T) {
|
||||
delaySeconds := -1
|
||||
expected := 0
|
||||
delaySeconds = queueObj.handleDelaySeconds(delaySeconds)
|
||||
if delaySeconds != expected {
|
||||
t.Errorf("Expected %d, but now got %d", expected, delaySeconds)
|
||||
}
|
||||
|
||||
delaySeconds = 10
|
||||
expected = 10
|
||||
delaySeconds = queueObj.handleDelaySeconds(delaySeconds)
|
||||
if delaySeconds != expected {
|
||||
t.Errorf("Expected %d, but now got %d", expected, delaySeconds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlePollingWaitSeconds(t *testing.T) {
|
||||
pollingWaitSeconds := -1
|
||||
expected := 0
|
||||
pollingWaitSeconds = queueObj.handlePollingWaitSeconds(pollingWaitSeconds)
|
||||
if pollingWaitSeconds != expected {
|
||||
t.Errorf("Expected %d, but now got %d", expected, pollingWaitSeconds)
|
||||
}
|
||||
|
||||
pollingWaitSeconds = 100
|
||||
expected = 30
|
||||
pollingWaitSeconds = queueObj.handlePollingWaitSeconds(pollingWaitSeconds)
|
||||
if pollingWaitSeconds != expected {
|
||||
t.Errorf("Expected %d, but now got %d", expected, pollingWaitSeconds)
|
||||
}
|
||||
|
||||
pollingWaitSeconds = 15
|
||||
expected = 15
|
||||
pollingWaitSeconds = queueObj.handlePollingWaitSeconds(pollingWaitSeconds)
|
||||
if pollingWaitSeconds != expected {
|
||||
t.Errorf("Expected %d, but now got %d", expected, pollingWaitSeconds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidBatchList(t *testing.T) {
|
||||
var list []string
|
||||
expected := EMPTY_BATCH_LIST_ERROR
|
||||
err := queueObj.validBatchList(list)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't")
|
||||
}
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Expected %s, but now got %s", EMPTY_BATCH_LIST_ERROR, err.Error())
|
||||
}
|
||||
|
||||
list = make([]string, 0, 16)
|
||||
expected = EMPTY_BATCH_LIST_ERROR
|
||||
err = queueObj.validBatchList(list)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't")
|
||||
}
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Expected %s, but now got %s", EMPTY_BATCH_LIST_ERROR, err.Error())
|
||||
}
|
||||
|
||||
for i := 0; i <= 2*MAX_BATCH_COUNT; i++ {
|
||||
list = append(list, fmt.Sprintf("Test.%d", i))
|
||||
}
|
||||
|
||||
expected = EXCEED_MAX_BATCH_COUNT_ERROR
|
||||
err = queueObj.validBatchList(list)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there isn't")
|
||||
}
|
||||
if err.Error() != expected {
|
||||
t.Errorf("Expected %s, but now got %s", EMPTY_BATCH_LIST_ERROR, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendMessage(t *testing.T) {
|
||||
message := "这是测试内容. Test"
|
||||
|
||||
// SendMessage
|
||||
err := queueObj.SendMessage(message, 0)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// ReceiveMessage
|
||||
receiptHandle, actualMessage, exist, err := queueObj.ReceiveMessage(3)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is: %s", err)
|
||||
return
|
||||
}
|
||||
if exist == false {
|
||||
t.Errorf("There should be one message, but now it's empty.")
|
||||
return
|
||||
}
|
||||
if message != actualMessage {
|
||||
t.Errorf("Expected %s, but got %s", message, actualMessage)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteMessage
|
||||
err = queueObj.DeleteMessage(receiptHandle)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify
|
||||
_, _, exist, err = queueObj.ReceiveMessage(3)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is: %s", err)
|
||||
return
|
||||
}
|
||||
if exist {
|
||||
t.Errorf("There should be no message, but now there is.")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestBatchSendMessage(t *testing.T) {
|
||||
count := 5
|
||||
messageList := make([]string, 0, count)
|
||||
for i := 0; i < count; i++ {
|
||||
messageList = append(messageList, fmt.Sprintf("This is for test:%d", i+1))
|
||||
}
|
||||
|
||||
// BatchSendMessage
|
||||
err := queueObj.BatchSendMessage(messageList, 0)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// BatchReceiveMessage
|
||||
receiptHandleList, retMessageList, exist, err := queueObj.BatchReceiveMessage(count, 3)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
if !exist || messageList == nil || len(messageList) != count {
|
||||
t.Errorf("There should be %d messages, but now there isn't.", count)
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
if messageList[i] != retMessageList[i] {
|
||||
t.Errorf("Expected %s, but got %s", messageList[i], retMessageList[i])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// BatchDeleteMessage
|
||||
_, err = queueObj.BatchDeleteMessage(receiptHandleList)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is:%s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify
|
||||
_, _, exist, err = queueObj.ReceiveMessage(3)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is: %s", err)
|
||||
return
|
||||
}
|
||||
if exist {
|
||||
t.Errorf("There should be no message, but now there is.")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package monitorMgr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// 监控配置对象
|
||||
type MonitorConfig struct {
|
||||
// 监控使用的服务器IP
|
||||
ServerIp string
|
||||
|
||||
// 监控使用的服务器名称
|
||||
ServerName string
|
||||
|
||||
// 监控的时间间隔(单位:分钟)
|
||||
Interval int
|
||||
}
|
||||
|
||||
func (this *MonitorConfig) String() string {
|
||||
bytes, _ := json.Marshal(this)
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func NewMonitorConfig(serverIp, serverName string, interval int) *MonitorConfig {
|
||||
return &MonitorConfig{
|
||||
ServerIp: serverIp,
|
||||
ServerName: serverName,
|
||||
Interval: interval,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user