net_cache_test.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. package service
  2. /**
  3. * 需要缓存的对象
  4. ******************************************
  5. * one2many
  6. * key type val
  7. * flow int64+avail []*tokenbind,err---ok
  8. * flow int64+dir []*dir,err
  9. * netid int64 []*flow,err --- ok
  10. * netid []int64,avail,disp []int64,err--transitionid
  11. * tranid []int64,isbatch []*tokenbind,err---ok
  12. * tranid int64,avail []*dir.err
  13. * biz int64 []int64,err---netid ---ok
  14. *
  15. * one2one
  16. * ids []int64,xxx,yy []*struct,err --- net/bind/flow/=ok
  17. ******************************************
  18. * testing:
  19. * 1. get objs by ids
  20. * 2. get objs by obj.aggregate_field
  21. * 3. get obj ids by obj.aggregate_field
  22. */
  23. import (
  24. "context"
  25. "encoding/json"
  26. "fmt"
  27. "reflect"
  28. "strings"
  29. "testing"
  30. "github.com/smartystreets/goconvey/convey"
  31. "go-common/app/admin/main/aegis/model/net"
  32. "go-common/library/database/orm"
  33. )
  34. var (
  35. testaggrmap = map[int64]struct {
  36. Rela []int64
  37. JSON string
  38. }{
  39. 1: {Rela: []int64{1, 2}, JSON: "1,2"},
  40. 2: {Rela: []int64{3, 4}, JSON: "3,4"},
  41. }
  42. testaggrv = []int64{1, 2}
  43. testaggrcache = map[string]string{
  44. "test_aggr:1": "1,2",
  45. "test_aggr:2": "3,4",
  46. }
  47. testaggrdest = &AggrCacheDest{
  48. GetKey: func(id int64, opt ...interface{}) (k []string) {
  49. k = []string{fmt.Sprintf("test_aggr:%d", id)}
  50. return
  51. },
  52. AggrRelaRaw: func(c context.Context, miss []int64, kopt []interface{}, opt ...interface{}) (rela map[int64][]int64, err error) {
  53. rela = map[int64][]int64{}
  54. for _, i := range miss {
  55. rela[i] = testaggrmap[i].Rela
  56. }
  57. return
  58. },
  59. }
  60. testflows = []*net.Flow{
  61. {ID: 50, Name: "init", NetID: 99},
  62. {ID: 51, Name: "first", NetID: 99},
  63. {ID: 52, Name: "init", NetID: 100},
  64. }
  65. testnets = []*net.Net{
  66. {ID: 8, ChName: "测试网1", BusinessID: 10},
  67. {ID: 9, ChName: "测试网1", BusinessID: 10},
  68. {ID: 10, ChName: "测试网1", BusinessID: 11},
  69. }
  70. testtokens = []*net.Token{
  71. {ID: 50, NetID: 10, Name: "test1", Compare: 0, Value: "0"},
  72. {ID: 51, NetID: 10, Name: "test2", Compare: 0, Value: "0"},
  73. {ID: 52, NetID: 11, Name: "test2", Compare: 0, Value: "0"},
  74. }
  75. testbinds = []*net.TokenBind{
  76. {ID: 50, Type: 1, ElementID: 10, TokenID: "8"},
  77. {ID: 51, Type: 1, ElementID: 10, TokenID: "9"},
  78. {ID: 52, Type: 2, ElementID: 11, TokenID: "8"},
  79. }
  80. testtrans = []*net.Transition{
  81. {ID: 50, NetID: 10, Trigger: 1, Limit: 1, Name: "test1"},
  82. {ID: 51, NetID: 10, Trigger: 1, Limit: 1, Name: "test2"},
  83. {ID: 52, NetID: 11, Trigger: 1, Limit: 1, Name: "test3"},
  84. }
  85. testdirs = []*net.Direction{
  86. {ID: 50, NetID: 10, FlowID: 10, TransitionID: 20, Direction: 1},
  87. {ID: 51, NetID: 10, FlowID: 11, TransitionID: 20, Direction: 2},
  88. {ID: 52, NetID: 10, FlowID: 11, TransitionID: 21, Direction: 1},
  89. {ID: 53, NetID: 10, FlowID: 12, TransitionID: 21, Direction: 2},
  90. }
  91. testdata = map[string]struct {
  92. RowObj interface{}
  93. RowObjLen int
  94. RowRela map[int64][]int64
  95. Caches map[string]string
  96. AggrID, ObjID []int64
  97. }{
  98. "flow": {
  99. RowObj: testflows,
  100. RowObjLen: 3,
  101. RowRela: map[int64][]int64{99: {50, 51}, 100: {52}},
  102. Caches: map[string]string{
  103. "net_flow:99": "50,51",
  104. "net_flow:100": "52",
  105. "flow:50": getjsonobj(testflows[0]),
  106. "flow:51": getjsonobj(testflows[1]),
  107. "flow:52": getjsonobj(testflows[2]),
  108. },
  109. AggrID: []int64{99, 100, 101},
  110. ObjID: []int64{50, 51, 52},
  111. },
  112. "net": {
  113. RowObj: testnets,
  114. RowObjLen: 3,
  115. RowRela: map[int64][]int64{10: {8, 9}, 11: {10}},
  116. Caches: map[string]string{
  117. "business_net:10": "8,9",
  118. "business_net:11": "10",
  119. "net:8": getjsonobj(testnets[0]),
  120. "net:9": getjsonobj(testnets[1]),
  121. "net:10": getjsonobj(testnets[2]),
  122. },
  123. AggrID: []int64{10, 11, 1200},
  124. ObjID: []int64{8, 9, 10},
  125. },
  126. "token": {
  127. RowObj: testtokens,
  128. RowObjLen: 3,
  129. Caches: map[string]string{
  130. "token:50": getjsonobj(testtokens[0]),
  131. "token:51": getjsonobj(testtokens[1]),
  132. "token:52": getjsonobj(testtokens[2]),
  133. },
  134. ObjID: []int64{50, 51, 52},
  135. },
  136. "bind": {
  137. RowObj: testbinds,
  138. RowObjLen: 3,
  139. RowRela: map[int64][]int64{10: {50, 51}, 11: {52}},
  140. Caches: map[string]string{
  141. "flow_bind:10": "50,51",
  142. "transition_bind:11": "52",
  143. "bind:50": getjsonobj(testbinds[0]),
  144. "bind:51": getjsonobj(testbinds[1]),
  145. "bind:52": getjsonobj(testbinds[2]),
  146. },
  147. AggrID: []int64{10, 11, 1200},
  148. ObjID: []int64{50, 51, 52},
  149. },
  150. "tran": {
  151. RowObj: testtrans,
  152. RowObjLen: 3,
  153. RowRela: map[int64][]int64{10: {50, 51}, 11: {52}},
  154. Caches: map[string]string{
  155. "net_transition:10": "50,51",
  156. "net_transition:11": "52",
  157. "transition:50": getjsonobj(testtrans[0]),
  158. "transition:51": getjsonobj(testtrans[1]),
  159. "transition:52": getjsonobj(testtrans[2]),
  160. },
  161. AggrID: []int64{10, 11, 12000},
  162. ObjID: []int64{50, 51, 52},
  163. },
  164. }
  165. testdata2 = map[string]struct {
  166. RowObj interface{}
  167. RowObjLen int
  168. RowRela map[int64]map[int8][]int64
  169. Caches map[string]string
  170. ObjID []int64
  171. AggrID []int64
  172. }{
  173. "dir": {
  174. RowObj: testdirs,
  175. RowObjLen: 4,
  176. RowRela: map[int64]map[int8][]int64{
  177. 10: {1: {50}},
  178. 11: {1: {52}, 2: {51}},
  179. 12: {2: {53}},
  180. 20: {1: {50}, 2: {51}},
  181. 21: {1: {52}, 2: {53}},
  182. },
  183. Caches: map[string]string{
  184. "flow_direction_1:10": "50",
  185. "flow_direction_1:11": "52",
  186. "flow_direction_2:11": "51",
  187. "flow_direction_2:12": "53",
  188. "transition_direction_1:20": "50",
  189. "transition_direction_2:20": "51",
  190. "transition_direction_1:21": "52",
  191. "transition_direction_2:21": "53",
  192. "direction:50": getjsonobj(testdirs[0]),
  193. "direction:51": getjsonobj(testdirs[1]),
  194. "direction:52": getjsonobj(testdirs[2]),
  195. "direction:53": getjsonobj(testdirs[3]),
  196. },
  197. AggrID: []int64{10, 11, 12, 20, 21, 12000},
  198. ObjID: []int64{50, 51, 52, 53},
  199. },
  200. }
  201. getjsonobj = func(v interface{}) string {
  202. bs, _ := json.Marshal(v)
  203. return string(bs)
  204. }
  205. )
  206. func TestServiceAppendString(t *testing.T) {
  207. convey.Convey("AppendString", t, func(ctx convey.C) {
  208. testaggrdest.Dest = map[int64][]int64{}
  209. ctx.Convey("err=nil, ok=true, aggrdest expected", func(ctx convey.C) {
  210. for id, item := range testaggrmap {
  211. ok, err := testaggrdest.AppendString(id, item.JSON)
  212. ctx.So(err, convey.ShouldBeNil)
  213. ctx.So(ok, convey.ShouldEqual, true)
  214. ctx.So(fmt.Sprintf("%v", testaggrdest.Dest[id]), convey.ShouldEqual, fmt.Sprintf("%v", item.Rela))
  215. }
  216. })
  217. })
  218. }
  219. func TestServiceAppendRelationRaw(t *testing.T) {
  220. convey.Convey("AppendRelationRaw", t, func(ctx convey.C) {
  221. missCache, err := testaggrdest.AppendRelationRaw(cntx, testaggrv, nil)
  222. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  223. ctx.So(err, convey.ShouldBeNil)
  224. ctx.So(missCache, convey.ShouldNotBeNil)
  225. for k, v := range missCache {
  226. ctx.So(v, convey.ShouldEqual, testaggrcache[k])
  227. }
  228. })
  229. })
  230. }
  231. func testdataprepare(k string, v interface{}) {
  232. testdao := orm.NewMySQL(s.c.ORM)
  233. objs := reflect.ValueOf(v)
  234. for i := 0; i < objs.Len(); i++ {
  235. testdao.Save(objs.Index(i).Interface())
  236. }
  237. }
  238. func TestServiceAppendAggregateRaw(t *testing.T) {
  239. convey.Convey("AppendAggregateRaw", t, func(ctx convey.C) {
  240. objdest := &flowArr{}
  241. w := &CacheWrap{
  242. ObjDest: objdest,
  243. AggrObjRaw: s.netFlowAppendRaw,
  244. AggregateDest: &AggrCacheDest{
  245. GetKey: s.netFlowKey,
  246. AggrRelaRaw: s.netFlowRelation,
  247. },
  248. }
  249. k := "flow"
  250. testdataprepare(k, testdata[k].RowObj)
  251. missCache, err := w.AppendAggregateRaw(cntx, testdata[k].AggrID, nil)
  252. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  253. ctx.So(err, convey.ShouldBeNil)
  254. ctx.So(missCache, convey.ShouldNotBeNil)
  255. //对象存在 & 聚合关系存在 & 待缓存值存在
  256. ctx.So(len(objdest.dest), convey.ShouldEqual, testdata[k].RowObjLen)
  257. for ck, v := range testdata[k].RowRela {
  258. ctx.So(fmt.Sprintf("%v", w.AggregateDest.Dest[ck]), convey.ShouldEqual, fmt.Sprintf("%v", v))
  259. }
  260. for ck, v := range testdata[k].Caches {
  261. pos := strings.Index(missCache[ck], "ctime") //json化的
  262. if pos == -1 {
  263. ctx.So(missCache[ck], convey.ShouldEqual, v)
  264. } else {
  265. ctx.So(missCache[ck][:pos], convey.ShouldEqual, v[:pos])
  266. }
  267. }
  268. })
  269. })
  270. }
  271. func TestServiceGetFromCache(t *testing.T) {
  272. convey.Convey("getFromCache", t, func(ctx convey.C) {
  273. k := "flow"
  274. //delete cache
  275. keypro := s.netFlowKey
  276. llen := len(testdata[k].AggrID) + 2
  277. keys := []string{}
  278. ids := append(testdata[k].AggrID, 1000, 2000)
  279. for _, id := range ids {
  280. keys = append(keys, keypro(id)...)
  281. }
  282. s.redis.DelMulti(cntx, keys...)
  283. //miss all
  284. miss := s.getFromCache(cntx, ids, keypro, &flowArr{}, nil)
  285. //set last 2 caches & get from cache
  286. s.redis.SetMulti(cntx, map[string]string{keys[llen-1]: "10000", keys[llen-2]: "20000"})
  287. miss2 := s.getFromCache(cntx, ids, keypro, &AggrCacheDest{}, nil)
  288. ctx.Convey("Then miss should not be nil.", func(ctx convey.C) {
  289. ctx.So(miss, convey.ShouldNotBeNil)
  290. ctx.So(fmt.Sprintf("%v", miss), convey.ShouldEqual, fmt.Sprintf("%v", ids))
  291. ctx.So(fmt.Sprintf("%v", miss2), convey.ShouldEqual, fmt.Sprintf("%v", testdata[k].AggrID))
  292. })
  293. })
  294. }
  295. func TestServiceobjCache(t *testing.T) {
  296. convey.Convey("objCache", t, func(ctx convey.C) {
  297. k := "flow"
  298. ids := testdata[k].ObjID
  299. objdest := &flowArr{}
  300. err := s.objCache(cntx, ids, objdest, nil)
  301. objm := map[int64]*net.Flow{}
  302. for _, item := range objdest.dest {
  303. objm[item.ID] = item
  304. }
  305. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  306. ctx.So(err, convey.ShouldBeNil)
  307. for _, id := range ids {
  308. ctx.So(objm[id], convey.ShouldNotBeNil)
  309. }
  310. })
  311. })
  312. }
  313. func TestServiceaggregateRelationCache(t *testing.T) {
  314. convey.Convey("aggregateRelationCache", t, func(ctx convey.C) {
  315. k := "flow"
  316. aggrdest := &AggrCacheDest{
  317. GetKey: s.netFlowKey,
  318. AggrRelaRaw: s.netFlowRelation,
  319. }
  320. err := s.aggregateRelationCache(cntx, testdata[k].AggrID, aggrdest, nil)
  321. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  322. ctx.So(err, convey.ShouldBeNil)
  323. for nid, v := range testdata[k].RowRela {
  324. ctx.So(len(aggrdest.Dest[nid]), convey.ShouldEqual, len(v))
  325. mmp := map[int64]int64{}
  326. for _, i := range v {
  327. mmp[i] = i
  328. }
  329. for _, i := range aggrdest.Dest[nid] {
  330. ctx.So(i, convey.ShouldEqual, mmp[i])
  331. }
  332. }
  333. })
  334. })
  335. }
  336. func TestServiceaggregateCache(t *testing.T) {
  337. convey.Convey("aggregateCache", t, func(ctx convey.C) {
  338. k := "flow"
  339. objdest := &flowArr{}
  340. w := &CacheWrap{
  341. ObjDest: objdest,
  342. AggrObjRaw: s.netFlowAppendRaw,
  343. AggregateDest: &AggrCacheDest{
  344. GetKey: s.netFlowKey,
  345. },
  346. }
  347. err := s.aggregateCache(cntx, testdata[k].AggrID, w, nil, nil, nil, nil)
  348. objm := map[int64]*net.Flow{}
  349. for _, item := range objdest.dest {
  350. objm[item.ID] = item
  351. }
  352. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  353. ctx.So(err, convey.ShouldBeNil)
  354. for id, v := range testdata[k].RowRela {
  355. ctx.So(len(w.AggregateDest.Dest[id]), convey.ShouldEqual, len(v))
  356. mmp := map[int64]int64{}
  357. for _, i := range v {
  358. mmp[i] = i
  359. }
  360. for _, i := range w.AggregateDest.Dest[id] {
  361. ctx.So(i, convey.ShouldEqual, mmp[i])
  362. }
  363. }
  364. for _, id := range testdata[k].ObjID {
  365. ctx.So(objm[id], convey.ShouldNotBeNil)
  366. }
  367. })
  368. })
  369. }
  370. func TestServicedelCache(t *testing.T) {
  371. convey.Convey("delCache", t, func(ctx convey.C) {
  372. err := s.delCache(cntx, []string{})
  373. ctx.Convey("Then err should be nil.", func(ctx convey.C) {
  374. ctx.So(err, convey.ShouldBeNil)
  375. })
  376. })
  377. }
  378. func TestNetArrGetKey(t *testing.T) {
  379. convey.Convey("netArrGetKey", t, func(ctx convey.C) {
  380. objdest := &netArr{}
  381. p1 := objdest.GetKey(0)
  382. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  383. ctx.So(p1, convey.ShouldNotBeNil)
  384. })
  385. })
  386. }
  387. func TestNetArrAppendString(t *testing.T) {
  388. convey.Convey("netArrAppendString", t, func(ctx convey.C) {
  389. objdest := &netArr{}
  390. k := "net"
  391. testdataprepare(k, testdata[k].RowObj)
  392. v := testdata[k].Caches[objdest.GetKey(testdata[k].ObjID[0])[0]]
  393. ok, err := objdest.AppendString(testdata[k].ObjID[0], v)
  394. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  395. ctx.So(err, convey.ShouldBeNil)
  396. ctx.So(ok, convey.ShouldEqual, true)
  397. ctx.So(objdest.dest[0].ID, convey.ShouldEqual, testdata[k].ObjID[0])
  398. })
  399. })
  400. }
  401. func TestNetArrAppendRaw(t *testing.T) {
  402. convey.Convey("netArrAppendRaw", t, func(ctx convey.C) {
  403. objdest := &netArr{}
  404. k := "net"
  405. missCache, err := objdest.AppendRaw(cntx, s, testdata[k].ObjID, nil)
  406. objm := map[int64]*net.Net{}
  407. for _, item := range objdest.dest {
  408. objm[item.ID] = item
  409. }
  410. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  411. ctx.So(err, convey.ShouldBeNil)
  412. for ck, v := range testdata[k].Caches {
  413. if strings.Contains(ck, "business_net") {
  414. continue
  415. }
  416. pos := strings.Index(v, "ctime")
  417. if pos == -1 {
  418. ctx.So(fmt.Sprintf("%v", missCache[ck]), convey.ShouldEqual, fmt.Sprintf("%v", v))
  419. } else {
  420. ctx.So(fmt.Sprintf("%v", missCache[ck][:pos]), convey.ShouldEqual, fmt.Sprintf("%v", v[:pos]))
  421. }
  422. }
  423. for _, id := range testdata[k].ObjID {
  424. ctx.So(objm[id], convey.ShouldNotBeNil)
  425. }
  426. })
  427. })
  428. }
  429. func TestNetCache(t *testing.T) {
  430. convey.Convey("NetCache", t, func(ctx convey.C) {
  431. k := "net"
  432. biz := testdata[k].AggrID[0]
  433. res, err := s.netIDByBusiness(cntx, biz)
  434. ctx.So(err, convey.ShouldBeNil)
  435. ctx.So(fmt.Sprintf("%v", res), convey.ShouldEqual, fmt.Sprintf("%v", testdata[k].RowRela[biz]))
  436. id := testdata[k].ObjID[0]
  437. res2, err := s.netByID(cntx, id)
  438. ctx.So(err, convey.ShouldBeNil)
  439. ctx.So(res2.ID, convey.ShouldEqual, id)
  440. err = s.delNetCache(cntx, res2)
  441. ctx.So(err, convey.ShouldBeNil)
  442. res3, err := s.netIDByBusiness(cntx, biz)
  443. ctx.So(err, convey.ShouldBeNil)
  444. ctx.So(fmt.Sprintf("%v", res3), convey.ShouldEqual, fmt.Sprintf("%v", testdata[k].RowRela[biz]))
  445. res4, err := s.netByID(cntx, id)
  446. ctx.So(err, convey.ShouldBeNil)
  447. ctx.So(res4.ID, convey.ShouldEqual, id)
  448. })
  449. }
  450. func TestTokenArrGetKey(t *testing.T) {
  451. convey.Convey("TokenArrGetKey", t, func(ctx convey.C) {
  452. objdest := &tokenArr{}
  453. p1 := objdest.GetKey(0)
  454. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  455. ctx.So(p1, convey.ShouldNotBeNil)
  456. })
  457. })
  458. }
  459. func TestTokenArrAppendString(t *testing.T) {
  460. convey.Convey("TokenArrAppendString", t, func(ctx convey.C) {
  461. objdest := &tokenArr{}
  462. k := "token"
  463. testdataprepare(k, testdata[k].RowObj)
  464. v := testdata[k].Caches[objdest.GetKey(testdata[k].ObjID[0])[0]]
  465. ok, err := objdest.AppendString(testdata[k].ObjID[0], v)
  466. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  467. ctx.So(err, convey.ShouldBeNil)
  468. ctx.So(ok, convey.ShouldEqual, true)
  469. ctx.So(objdest.dest[0].ID, convey.ShouldEqual, testdata[k].ObjID[0])
  470. })
  471. })
  472. }
  473. func TestTokenArrAppendRaw(t *testing.T) {
  474. convey.Convey("TokenArrAppendRaw", t, func(ctx convey.C) {
  475. objdest := &tokenArr{}
  476. k := "token"
  477. missCache, err := objdest.AppendRaw(cntx, s, testdata[k].ObjID, nil)
  478. objm := map[int64]*net.Token{}
  479. for _, item := range objdest.dest {
  480. objm[item.ID] = item
  481. }
  482. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  483. ctx.So(err, convey.ShouldBeNil)
  484. for ck, v := range testdata[k].Caches {
  485. pos := strings.Index(v, "ctime")
  486. if pos == -1 {
  487. ctx.So(fmt.Sprintf("%v", missCache[ck]), convey.ShouldEqual, fmt.Sprintf("%v", v))
  488. } else {
  489. ctx.So(fmt.Sprintf("%v", missCache[ck][:pos]), convey.ShouldEqual, fmt.Sprintf("%v", v[:pos]))
  490. }
  491. }
  492. for _, id := range testdata[k].ObjID {
  493. ctx.So(objm[id], convey.ShouldNotBeNil)
  494. }
  495. })
  496. })
  497. }
  498. func TestTokenCache(t *testing.T) {
  499. convey.Convey("TokenCache", t, func(ctx convey.C) {
  500. k := "token"
  501. biz := testdata[k].ObjID
  502. res, err := s.tokens(cntx, biz)
  503. ctx.So(err, convey.ShouldBeNil)
  504. resm := map[int64]*net.Token{}
  505. for _, item := range res {
  506. resm[item.ID] = item
  507. }
  508. for _, i := range biz {
  509. ctx.So(resm[i], convey.ShouldNotBeNil)
  510. }
  511. keys := []string{}
  512. for ck := range testdata[k].Caches {
  513. keys = append(keys, ck)
  514. }
  515. err = s.redis.DelMulti(cntx, keys...)
  516. ctx.So(err, convey.ShouldBeNil)
  517. res2, err := s.tokens(cntx, biz)
  518. ctx.So(err, convey.ShouldBeNil)
  519. resm2 := map[int64]*net.Token{}
  520. for _, item := range res2 {
  521. resm2[item.ID] = item
  522. }
  523. for _, i := range biz {
  524. ctx.So(resm2[i], convey.ShouldNotBeNil)
  525. }
  526. })
  527. }
  528. func TestBindArrGetKey(t *testing.T) {
  529. convey.Convey("BindArrGetKey", t, func(ctx convey.C) {
  530. objdest := &bindArr{}
  531. p1 := objdest.GetKey(0)
  532. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  533. ctx.So(p1, convey.ShouldNotBeNil)
  534. })
  535. })
  536. }
  537. func TestBindArrAppendString(t *testing.T) {
  538. convey.Convey("BindArrAppendString", t, func(ctx convey.C) {
  539. objdest := &bindArr{}
  540. k := "bind"
  541. testdataprepare(k, testdata[k].RowObj)
  542. v := testdata[k].Caches[objdest.GetKey(testdata[k].ObjID[0])[0]]
  543. ok, err := objdest.AppendString(testdata[k].ObjID[0], v)
  544. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  545. ctx.So(err, convey.ShouldBeNil)
  546. ctx.So(ok, convey.ShouldEqual, true)
  547. ctx.So(objdest.dest[0].ID, convey.ShouldEqual, testdata[k].ObjID[0])
  548. })
  549. })
  550. }
  551. func TestBindArrAppendRaw(t *testing.T) {
  552. convey.Convey("BindArrAppendRaw", t, func(ctx convey.C) {
  553. objdest := &bindArr{}
  554. k := "bind"
  555. missCache, err := objdest.AppendRaw(cntx, s, testdata[k].ObjID, nil)
  556. objm := map[int64]*net.TokenBind{}
  557. for _, item := range objdest.dest {
  558. objm[item.ID] = item
  559. }
  560. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  561. ctx.So(err, convey.ShouldBeNil)
  562. for ck, v := range testdata[k].Caches {
  563. if strings.Contains(ck, "flow_bind") || strings.Contains(ck, "transition_bind") {
  564. continue
  565. }
  566. pos := strings.Index(v, "ctime")
  567. if pos == -1 {
  568. ctx.So(fmt.Sprintf("%v", missCache[ck]), convey.ShouldEqual, fmt.Sprintf("%v", v))
  569. } else {
  570. ctx.So(fmt.Sprintf("%v", missCache[ck][:pos]), convey.ShouldEqual, fmt.Sprintf("%v", v[:pos]))
  571. }
  572. }
  573. for _, id := range testdata[k].ObjID {
  574. ctx.So(objm[id], convey.ShouldNotBeNil)
  575. }
  576. })
  577. })
  578. }
  579. func TestBindCache(t *testing.T) {
  580. convey.Convey("BindCache", t, func(ctx convey.C) {
  581. k := "bind"
  582. biz := testdata[k].AggrID
  583. res, err := s.tokenBindByElement(cntx, biz, []int8{net.BindTypeFlow})
  584. resm := map[int64]*net.TokenBind{}
  585. for _, item := range res {
  586. t.Logf("flow bind tokens in biz(%+v) item(%+v)", biz, item)
  587. resm[item.ID] = item
  588. }
  589. ctx.So(err, convey.ShouldBeNil)
  590. for _, i := range testdata[k].ObjID[:2] {
  591. ctx.So(resm[i], convey.ShouldNotBeNil)
  592. }
  593. res, err = s.tokenBindByElement(cntx, biz, net.BindTranType)
  594. for _, item := range res {
  595. resm[item.ID] = item
  596. }
  597. ctx.So(err, convey.ShouldBeNil)
  598. res2, err := s.tokenBinds(cntx, testdata[k].ObjID, true)
  599. resm2 := map[int64]*net.TokenBind{}
  600. for _, item := range res2 {
  601. resm2[item.ID] = item
  602. }
  603. ctx.So(err, convey.ShouldBeNil)
  604. for _, i := range testdata[k].ObjID {
  605. ctx.So(resm2[i], convey.ShouldNotBeNil)
  606. }
  607. })
  608. }
  609. func TestFlowArrGetKey(t *testing.T) {
  610. convey.Convey("FlowArrGetKey", t, func(ctx convey.C) {
  611. objdest := &flowArr{}
  612. p1 := objdest.GetKey(0)
  613. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  614. ctx.So(p1, convey.ShouldNotBeNil)
  615. })
  616. })
  617. }
  618. func TestFlowArrAppendString(t *testing.T) {
  619. convey.Convey("FlowArrAppendString", t, func(ctx convey.C) {
  620. objdest := &flowArr{}
  621. k := "flow"
  622. testdataprepare(k, testdata[k].RowObj)
  623. v := testdata[k].Caches[objdest.GetKey(testdata[k].ObjID[0])[0]]
  624. ok, err := objdest.AppendString(testdata[k].ObjID[0], v)
  625. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  626. ctx.So(err, convey.ShouldBeNil)
  627. ctx.So(ok, convey.ShouldEqual, true)
  628. ctx.So(objdest.dest[0].ID, convey.ShouldEqual, testdata[k].ObjID[0])
  629. })
  630. })
  631. }
  632. func TestFlowArrAppendRaw(t *testing.T) {
  633. convey.Convey("FlowArrAppendRaw", t, func(ctx convey.C) {
  634. objdest := &flowArr{}
  635. k := "flow"
  636. missCache, err := objdest.AppendRaw(cntx, s, testdata[k].ObjID, nil)
  637. objm := map[int64]*net.Flow{}
  638. for _, item := range objdest.dest {
  639. objm[item.ID] = item
  640. }
  641. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  642. ctx.So(err, convey.ShouldBeNil)
  643. for ck, v := range testdata[k].Caches {
  644. if strings.Contains(ck, "net_flow") {
  645. continue
  646. }
  647. pos := strings.Index(v, "ctime")
  648. if pos == -1 {
  649. ctx.So(fmt.Sprintf("%v", missCache[ck]), convey.ShouldEqual, fmt.Sprintf("%v", v))
  650. } else {
  651. ctx.So(fmt.Sprintf("%v", missCache[ck][:pos]), convey.ShouldEqual, fmt.Sprintf("%v", v[:pos]))
  652. }
  653. }
  654. for _, id := range testdata[k].ObjID {
  655. ctx.So(objm[id], convey.ShouldNotBeNil)
  656. }
  657. })
  658. })
  659. }
  660. func TestFlowCache(t *testing.T) {
  661. convey.Convey("FlowCache", t, func(ctx convey.C) {
  662. k := "flow"
  663. biz := testdata[k].AggrID[0]
  664. res, err := s.flowsByNet(cntx, biz)
  665. resm := map[int64]*net.Flow{}
  666. for _, item := range res {
  667. resm[item.ID] = item
  668. }
  669. ctx.So(err, convey.ShouldBeNil)
  670. for _, i := range testdata[k].RowRela[biz] {
  671. ctx.So(resm[i], convey.ShouldNotBeNil)
  672. }
  673. res2, err := s.flowIDByNet(cntx, biz)
  674. ctx.So(err, convey.ShouldBeNil)
  675. mmp := map[int64]int64{}
  676. for _, i := range testdata[k].RowRela[biz] {
  677. mmp[i] = i
  678. }
  679. ctx.So(len(res2), convey.ShouldEqual, len(testdata[k].RowRela[biz]))
  680. for _, i := range res2 {
  681. ctx.So(i, convey.ShouldEqual, mmp[i])
  682. }
  683. res3, err := s.flows(cntx, testdata[k].ObjID, true)
  684. resm2 := map[int64]*net.Flow{}
  685. for _, item := range res3 {
  686. resm2[item.ID] = item
  687. }
  688. ctx.So(err, convey.ShouldBeNil)
  689. for _, i := range testdata[k].ObjID {
  690. ctx.So(resm2[i], convey.ShouldNotBeNil)
  691. }
  692. })
  693. }
  694. func TestTranArrGetKey(t *testing.T) {
  695. convey.Convey("TranArrGetKey", t, func(ctx convey.C) {
  696. objdest := &tranArr{}
  697. p1 := objdest.GetKey(0)
  698. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  699. ctx.So(p1, convey.ShouldNotBeNil)
  700. })
  701. })
  702. }
  703. func TestTranArrAppendString(t *testing.T) {
  704. convey.Convey("TranArrAppendString", t, func(ctx convey.C) {
  705. objdest := &tranArr{}
  706. k := "tran"
  707. testdataprepare(k, testdata[k].RowObj)
  708. v := testdata[k].Caches[objdest.GetKey(testdata[k].ObjID[0])[0]]
  709. ok, err := objdest.AppendString(testdata[k].ObjID[0], v)
  710. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  711. ctx.So(err, convey.ShouldBeNil)
  712. ctx.So(ok, convey.ShouldEqual, true)
  713. ctx.So(objdest.dest[0].ID, convey.ShouldEqual, testdata[k].ObjID[0])
  714. })
  715. })
  716. }
  717. func TestTranArrAppendRaw(t *testing.T) {
  718. convey.Convey("TranArrAppendRaw", t, func(ctx convey.C) {
  719. objdest := &tranArr{}
  720. k := "tran"
  721. _, err := objdest.AppendRaw(cntx, s, testdata[k].ObjID, nil)
  722. objm := map[int64]*net.Transition{}
  723. for _, item := range objdest.dest {
  724. objm[item.ID] = item
  725. }
  726. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  727. ctx.So(err, convey.ShouldBeNil)
  728. for _, id := range testdata[k].ObjID {
  729. ctx.So(objm[id], convey.ShouldNotBeNil)
  730. }
  731. })
  732. })
  733. }
  734. func TestTranCache(t *testing.T) {
  735. convey.Convey("TranCache", t, func(ctx convey.C) {
  736. k := "tran"
  737. biz := testdata[k].AggrID[0]
  738. res2, err := s.tranIDByNet(cntx, []int64{biz}, true, true)
  739. ctx.So(err, convey.ShouldBeNil)
  740. ctx.So(fmt.Sprintf("%v", res2), convey.ShouldEqual, fmt.Sprintf("%v", testdata[k].RowRela[biz]))
  741. res3, err := s.transitions(cntx, testdata[k].ObjID, true)
  742. resm2 := map[int64]*net.Transition{}
  743. for _, item := range res3 {
  744. resm2[item.ID] = item
  745. }
  746. ctx.So(err, convey.ShouldBeNil)
  747. for _, i := range testdata[k].ObjID {
  748. ctx.So(resm2[i], convey.ShouldNotBeNil)
  749. }
  750. })
  751. }
  752. func TestDirArrGetKey(t *testing.T) {
  753. convey.Convey("dirArrGetKey", t, func(ctx convey.C) {
  754. objdest := &dirArr{}
  755. p1 := objdest.GetKey(0)
  756. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  757. ctx.So(p1, convey.ShouldNotBeNil)
  758. })
  759. })
  760. }
  761. func TestDirArrAppendString(t *testing.T) {
  762. convey.Convey("dirArrAppendString", t, func(ctx convey.C) {
  763. objdest := &dirArr{}
  764. k := "dir"
  765. testdataprepare(k, testdata2[k].RowObj)
  766. v := testdata2[k].Caches[objdest.GetKey(testdata2[k].ObjID[0])[0]]
  767. ok, err := objdest.AppendString(testdata2[k].ObjID[0], v)
  768. ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
  769. ctx.So(err, convey.ShouldBeNil)
  770. ctx.So(ok, convey.ShouldEqual, true)
  771. ctx.So(objdest.dest[0].ID, convey.ShouldEqual, testdata2[k].ObjID[0])
  772. })
  773. })
  774. }
  775. func TestDirArrAppendRaw(t *testing.T) {
  776. convey.Convey("dirArrAppendRaw", t, func(ctx convey.C) {
  777. objdest := &dirArr{}
  778. k := "dir"
  779. missCache, err := objdest.AppendRaw(cntx, s, testdata2[k].ObjID, nil)
  780. objm := map[int64]*net.Direction{}
  781. for _, item := range objdest.dest {
  782. objm[item.ID] = item
  783. }
  784. ctx.Convey("Then err should be nil.missCache should not be nil.", func(ctx convey.C) {
  785. ctx.So(err, convey.ShouldBeNil)
  786. for ck, v := range testdata2[k].Caches {
  787. if strings.Contains(ck, "flow_direction") || strings.Contains(ck, "transition_direction") {
  788. continue
  789. }
  790. pos := strings.Index(v, "ctime")
  791. if pos == -1 {
  792. ctx.So(fmt.Sprintf("%v", missCache[ck]), convey.ShouldEqual, fmt.Sprintf("%v", v))
  793. } else {
  794. ctx.So(fmt.Sprintf("%v", missCache[ck][:pos]), convey.ShouldEqual, fmt.Sprintf("%v", v[:pos]))
  795. }
  796. }
  797. for _, id := range testdata2[k].ObjID {
  798. ctx.So(objm[id], convey.ShouldNotBeNil)
  799. }
  800. })
  801. })
  802. }
  803. func TestDirCache(t *testing.T) {
  804. convey.Convey("DirCache", t, func(ctx convey.C) {
  805. k := "dir"
  806. biz := testdata2[k].AggrID
  807. tp := net.DirInput
  808. res, err := s.dirByFlow(cntx, biz, tp)
  809. resm := map[int64]*net.Direction{}
  810. for _, item := range res {
  811. resm[item.ID] = item
  812. }
  813. ctx.So(err, convey.ShouldBeNil)
  814. expected := []int64{}
  815. for _, i := range biz {
  816. expected = append(expected, testdata2[k].RowRela[i][tp]...)
  817. }
  818. for _, i := range expected {
  819. ctx.So(resm[i], convey.ShouldNotBeNil)
  820. }
  821. res2, err := s.dirByTran(cntx, biz, net.DirOutput, true)
  822. for _, item := range res2 {
  823. resm[item.ID] = item
  824. }
  825. ctx.So(err, convey.ShouldBeNil)
  826. expected2 := []int64{}
  827. for _, i := range biz {
  828. expected2 = append(expected2, testdata2[k].RowRela[i][tp]...)
  829. }
  830. for _, i := range expected2 {
  831. ctx.So(resm[i], convey.ShouldNotBeNil)
  832. }
  833. res3, err := s.directions(cntx, testdata2[k].ObjID)
  834. resm2 := map[int64]*net.Direction{}
  835. for _, item := range res3 {
  836. resm2[item.ID] = item
  837. }
  838. ctx.So(err, convey.ShouldBeNil)
  839. for _, i := range testdata2[k].ObjID {
  840. ctx.So(resm2[i], convey.ShouldNotBeNil)
  841. }
  842. })
  843. }