Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package coroutine_timer
|
||||
|
||||
// timersModel
|
||||
// @description: timer卡槽对象
|
||||
type timersModel struct {
|
||||
timers map[string]*timerObj
|
||||
}
|
||||
|
||||
// newTimersModel
|
||||
// @description: 构造timer卡槽对象
|
||||
// parameter:
|
||||
// return:
|
||||
// @*timersModel:
|
||||
func newTimersModel() *timersModel {
|
||||
return &timersModel{timers: map[string]*timerObj{}}
|
||||
}
|
||||
|
||||
// addTimer
|
||||
// @description: 添加定时器
|
||||
// parameter:
|
||||
// @receiver this:
|
||||
// @t:
|
||||
// return:
|
||||
func (this *timersModel) addTimer(t *timerObj) {
|
||||
this.timers[t.id] = t
|
||||
}
|
||||
|
||||
// delTimer
|
||||
// @description: 删除定时器
|
||||
// parameter:
|
||||
// @receiver this:
|
||||
// @id:
|
||||
// return:
|
||||
func (this *timersModel) delTimer(id string) {
|
||||
delete(this.timers, id)
|
||||
}
|
||||
|
||||
// exist
|
||||
// @description: 判断id是否存在
|
||||
// parameter:
|
||||
// @receiver this:
|
||||
// @id:
|
||||
// return:
|
||||
// @exist:
|
||||
func (this *timersModel) exist(id string) (exist bool) {
|
||||
_, exist = this.timers[id]
|
||||
return
|
||||
}
|
||||
|
||||
// getAllTimers
|
||||
// @description: 获取所有定时器
|
||||
// parameter:
|
||||
// @receiver this:
|
||||
// return:
|
||||
// @map[string]*timerObj:
|
||||
func (this *timersModel) getAllTimers() map[string]*timerObj {
|
||||
return this.timers
|
||||
}
|
||||
|
||||
// getAllTimers2
|
||||
// @description: 获取所有定时器
|
||||
// parameter:
|
||||
// @receiver this:
|
||||
// return:
|
||||
// @result:
|
||||
func (this *timersModel) getAllTimers2() (result []*timerObj) {
|
||||
result = make([]*timerObj, 0, len(this.timers))
|
||||
for _, v := range this.timers {
|
||||
result = append(result, v)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package rank_util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"goutil/mathUtil"
|
||||
"math"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Rank(t *testing.T) {
|
||||
r := NewRankUtil(20, compar)
|
||||
|
||||
dc := make(map[string]*rmodel)
|
||||
for i := 1; i < 1000; i++ {
|
||||
tk := strconv.Itoa(i)
|
||||
m := &rmodel{k: tk, Fap: 0}
|
||||
dc[m.k] = m
|
||||
}
|
||||
|
||||
goCount := 10
|
||||
srand := mathUtil.GetRand()
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(goCount)
|
||||
for i := 0; i < goCount; i++ {
|
||||
go func() {
|
||||
for i := 1; i < 1000; i++ {
|
||||
tk := strconv.Itoa(i)
|
||||
newf := srand.GetRandRangeInt(1, 1000)
|
||||
|
||||
m, _ := dc[tk]
|
||||
isUp := m.Fap < newf
|
||||
m.Fap = newf
|
||||
r.Refresh(tk, m, isUp)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
items := r.GetAll()
|
||||
if len(items) != r.maxCount {
|
||||
t.Error("排行榜长度不为", r.maxCount)
|
||||
}
|
||||
|
||||
//测试删除
|
||||
r.Delete(r.GetItemForRank(1).key)
|
||||
items = r.GetAll()
|
||||
if len(items) != r.maxCount-1 {
|
||||
t.Error("删除后的排行榜长度不为", r.maxCount-1)
|
||||
}
|
||||
|
||||
//校验标准: 1.key不可以重复 2.fap越来越小
|
||||
beforeFap := math.MaxInt
|
||||
tempKeyDict := make(map[string]bool)
|
||||
isPrintAll := false
|
||||
for _, item := range items {
|
||||
m := item.obj.(*rmodel)
|
||||
if m.k != item.key {
|
||||
t.Errorf("错误:item.key!=m.k item.key:%s m.k:%s", item.key, m.k)
|
||||
isPrintAll = true
|
||||
}
|
||||
|
||||
// 校验1
|
||||
if _, exist := tempKeyDict[item.key]; exist {
|
||||
t.Errorf("错误:item.key重复 item.key:%s m.k:%s", item.key, m.k)
|
||||
isPrintAll = true
|
||||
} else {
|
||||
tempKeyDict[item.key] = false
|
||||
}
|
||||
|
||||
// 校验2
|
||||
if m.Fap > beforeFap {
|
||||
t.Errorf("错误:m.Fap大于前面的排行 beforeFap:%v curFap:%v", beforeFap, m.Fap)
|
||||
isPrintAll = true
|
||||
}
|
||||
}
|
||||
|
||||
if isPrintAll {
|
||||
for _, item := range items {
|
||||
m := item.obj.(*rmodel)
|
||||
t.Log(m)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Rank2(t *testing.T) {
|
||||
r := NewRankUtil(5, compar)
|
||||
r.Refresh("1", &rmodel{k: "1", Fap: 1}, true)
|
||||
r.Refresh("2", &rmodel{k: "2", Fap: 2}, true)
|
||||
r.Refresh("3", &rmodel{k: "3", Fap: 3}, true)
|
||||
r.Refresh("4", &rmodel{k: "4", Fap: 4}, true)
|
||||
r.Refresh("5", &rmodel{k: "5", Fap: 5}, true)
|
||||
|
||||
//把1挤出去
|
||||
ischange, dm := r.Refresh("6", &rmodel{k: "6", Fap: 6}, true)
|
||||
if ischange == false {
|
||||
t.Errorf("把1挤出去错误,排行榜未变动")
|
||||
return
|
||||
}
|
||||
if dm.key != "1" {
|
||||
t.Errorf("把1挤出去错误,挤出的不是1")
|
||||
return
|
||||
}
|
||||
t.Log("被挤出的对象信息-> ", dm)
|
||||
|
||||
//打印所有
|
||||
items := r.GetAll()
|
||||
for _, item := range items {
|
||||
t.Log(item)
|
||||
}
|
||||
}
|
||||
|
||||
type rmodel struct {
|
||||
k string
|
||||
Fap int
|
||||
}
|
||||
|
||||
func (m *rmodel) String() string {
|
||||
return fmt.Sprintf("m.k:%s fap:%v", m.k, m.Fap)
|
||||
}
|
||||
|
||||
// compar
|
||||
// @description: 判断对象大小,返回含义 -1:a<b 0:a=b 1:a>b
|
||||
// parameter:
|
||||
//
|
||||
// @a:对象a
|
||||
// @b:对象b
|
||||
//
|
||||
// return:
|
||||
//
|
||||
// @int:
|
||||
func compar(a, b interface{}) int {
|
||||
af := a.(*rmodel).Fap
|
||||
bf := b.(*rmodel).Fap
|
||||
if af > bf {
|
||||
return 1
|
||||
} else if af == bf {
|
||||
return 0
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package shortUrlMgr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestShortUrl(t *testing.T) {
|
||||
// ShortUrl_SERVICE_URL = "http://a.app366.com/get"
|
||||
|
||||
appId := "unittest"
|
||||
appId_wrong := "wrong"
|
||||
appSecret := "c5746980-5d52-4ba9-834f-13d0066ad1d0"
|
||||
appSecret_wrong := "wrong"
|
||||
fullUrl := "http://unittest.PlayerId=123&Name=Jordan"
|
||||
fullUrl_wrong := ""
|
||||
timeout := 3
|
||||
|
||||
// Test with wrong AppId
|
||||
shortUrl, err := GetShortUrl(appId_wrong, appSecret, fullUrl, timeout)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there is no error")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with wrong AppSecret
|
||||
shortUrl, err = GetShortUrl(appId, appSecret_wrong, fullUrl, timeout)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there is no error")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with wrong FullUrl
|
||||
shortUrl, err = GetShortUrl(appId, appSecret, fullUrl_wrong, timeout)
|
||||
if err == nil {
|
||||
t.Errorf("There should be an error, but now there is no error")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with correct information and domestic ip
|
||||
shortUrl, err = GetShortUrl(appId, appSecret, fullUrl, timeout)
|
||||
if err != nil {
|
||||
t.Errorf("There should be no error, but now there is one:%s", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("shortUrl:%v\n", shortUrl)
|
||||
}
|
||||
Reference in New Issue
Block a user