No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cache_test.go 41KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  1. package cache
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "runtime"
  6. "strconv"
  7. "sync"
  8. "testing"
  9. "time"
  10. )
  11. type TestStruct struct {
  12. Num int
  13. Children []*TestStruct
  14. }
  15. func TestCache(t *testing.T) {
  16. tc := New(DefaultExpiration, 0)
  17. a, found := tc.Get("a")
  18. if found || a != nil {
  19. t.Error("Getting A found value that shouldn't exist:", a)
  20. }
  21. b, found := tc.Get("b")
  22. if found || b != nil {
  23. t.Error("Getting B found value that shouldn't exist:", b)
  24. }
  25. c, found := tc.Get("c")
  26. if found || c != nil {
  27. t.Error("Getting C found value that shouldn't exist:", c)
  28. }
  29. tc.Set("a", 1, DefaultExpiration)
  30. tc.Set("b", "b", DefaultExpiration)
  31. tc.Set("c", 3.5, DefaultExpiration)
  32. x, found := tc.Get("a")
  33. if !found {
  34. t.Error("a was not found while getting a2")
  35. }
  36. if x == nil {
  37. t.Error("x for a is nil")
  38. } else if a2 := x.(int); a2+2 != 3 {
  39. t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2)
  40. }
  41. x, found = tc.Get("b")
  42. if !found {
  43. t.Error("b was not found while getting b2")
  44. }
  45. if x == nil {
  46. t.Error("x for b is nil")
  47. } else if b2 := x.(string); b2+"B" != "bB" {
  48. t.Error("b2 (which should be b) plus B does not equal bB; value:", b2)
  49. }
  50. x, found = tc.Get("c")
  51. if !found {
  52. t.Error("c was not found while getting c2")
  53. }
  54. if x == nil {
  55. t.Error("x for c is nil")
  56. } else if c2 := x.(float64); c2+1.2 != 4.7 {
  57. t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
  58. }
  59. }
  60. func TestCacheTimes(t *testing.T) {
  61. var found bool
  62. tc := New(50*time.Millisecond, 1*time.Millisecond)
  63. tc.Set("a", 1, DefaultExpiration)
  64. tc.Set("b", 2, NoExpiration)
  65. tc.Set("c", 3, 20*time.Millisecond)
  66. tc.Set("d", 4, 70*time.Millisecond)
  67. <-time.After(25 * time.Millisecond)
  68. _, found = tc.Get("c")
  69. if found {
  70. t.Error("Found c when it should have been automatically deleted")
  71. }
  72. <-time.After(30 * time.Millisecond)
  73. _, found = tc.Get("a")
  74. if found {
  75. t.Error("Found a when it should have been automatically deleted")
  76. }
  77. _, found = tc.Get("b")
  78. if !found {
  79. t.Error("Did not find b even though it was set to never expire")
  80. }
  81. _, found = tc.Get("d")
  82. if !found {
  83. t.Error("Did not find d even though it was set to expire later than the default")
  84. }
  85. <-time.After(20 * time.Millisecond)
  86. _, found = tc.Get("d")
  87. if found {
  88. t.Error("Found d when it should have been automatically deleted (later than the default)")
  89. }
  90. }
  91. func TestNewFrom(t *testing.T) {
  92. m := map[string]Item{
  93. "a": {
  94. Object: 1,
  95. Expiration: 0,
  96. },
  97. "b": {
  98. Object: 2,
  99. Expiration: 0,
  100. },
  101. }
  102. tc := NewFrom(DefaultExpiration, 0, m)
  103. a, found := tc.Get("a")
  104. if !found {
  105. t.Fatal("Did not find a")
  106. }
  107. if a.(int) != 1 {
  108. t.Fatal("a is not 1")
  109. }
  110. b, found := tc.Get("b")
  111. if !found {
  112. t.Fatal("Did not find b")
  113. }
  114. if b.(int) != 2 {
  115. t.Fatal("b is not 2")
  116. }
  117. }
  118. func TestStorePointerToStruct(t *testing.T) {
  119. tc := New(DefaultExpiration, 0)
  120. tc.Set("foo", &TestStruct{Num: 1}, DefaultExpiration)
  121. x, found := tc.Get("foo")
  122. if !found {
  123. t.Fatal("*TestStruct was not found for foo")
  124. }
  125. foo := x.(*TestStruct)
  126. foo.Num++
  127. y, found := tc.Get("foo")
  128. if !found {
  129. t.Fatal("*TestStruct was not found for foo (second time)")
  130. }
  131. bar := y.(*TestStruct)
  132. if bar.Num != 2 {
  133. t.Fatal("TestStruct.Num is not 2")
  134. }
  135. }
  136. func TestIncrementWithInt(t *testing.T) {
  137. tc := New(DefaultExpiration, 0)
  138. tc.Set("tint", 1, DefaultExpiration)
  139. err := tc.Increment("tint", 2)
  140. if err != nil {
  141. t.Error("Error incrementing:", err)
  142. }
  143. x, found := tc.Get("tint")
  144. if !found {
  145. t.Error("tint was not found")
  146. }
  147. if x.(int) != 3 {
  148. t.Error("tint is not 3:", x)
  149. }
  150. }
  151. func TestIncrementWithInt8(t *testing.T) {
  152. tc := New(DefaultExpiration, 0)
  153. tc.Set("tint8", int8(1), DefaultExpiration)
  154. err := tc.Increment("tint8", 2)
  155. if err != nil {
  156. t.Error("Error incrementing:", err)
  157. }
  158. x, found := tc.Get("tint8")
  159. if !found {
  160. t.Error("tint8 was not found")
  161. }
  162. if x.(int8) != 3 {
  163. t.Error("tint8 is not 3:", x)
  164. }
  165. }
  166. func TestIncrementWithInt16(t *testing.T) {
  167. tc := New(DefaultExpiration, 0)
  168. tc.Set("tint16", int16(1), DefaultExpiration)
  169. err := tc.Increment("tint16", 2)
  170. if err != nil {
  171. t.Error("Error incrementing:", err)
  172. }
  173. x, found := tc.Get("tint16")
  174. if !found {
  175. t.Error("tint16 was not found")
  176. }
  177. if x.(int16) != 3 {
  178. t.Error("tint16 is not 3:", x)
  179. }
  180. }
  181. func TestIncrementWithInt32(t *testing.T) {
  182. tc := New(DefaultExpiration, 0)
  183. tc.Set("tint32", int32(1), DefaultExpiration)
  184. err := tc.Increment("tint32", 2)
  185. if err != nil {
  186. t.Error("Error incrementing:", err)
  187. }
  188. x, found := tc.Get("tint32")
  189. if !found {
  190. t.Error("tint32 was not found")
  191. }
  192. if x.(int32) != 3 {
  193. t.Error("tint32 is not 3:", x)
  194. }
  195. }
  196. func TestIncrementWithInt64(t *testing.T) {
  197. tc := New(DefaultExpiration, 0)
  198. tc.Set("tint64", int64(1), DefaultExpiration)
  199. err := tc.Increment("tint64", 2)
  200. if err != nil {
  201. t.Error("Error incrementing:", err)
  202. }
  203. x, found := tc.Get("tint64")
  204. if !found {
  205. t.Error("tint64 was not found")
  206. }
  207. if x.(int64) != 3 {
  208. t.Error("tint64 is not 3:", x)
  209. }
  210. }
  211. func TestIncrementWithUint(t *testing.T) {
  212. tc := New(DefaultExpiration, 0)
  213. tc.Set("tuint", uint(1), DefaultExpiration)
  214. err := tc.Increment("tuint", 2)
  215. if err != nil {
  216. t.Error("Error incrementing:", err)
  217. }
  218. x, found := tc.Get("tuint")
  219. if !found {
  220. t.Error("tuint was not found")
  221. }
  222. if x.(uint) != 3 {
  223. t.Error("tuint is not 3:", x)
  224. }
  225. }
  226. func TestIncrementWithUintptr(t *testing.T) {
  227. tc := New(DefaultExpiration, 0)
  228. tc.Set("tuintptr", uintptr(1), DefaultExpiration)
  229. err := tc.Increment("tuintptr", 2)
  230. if err != nil {
  231. t.Error("Error incrementing:", err)
  232. }
  233. x, found := tc.Get("tuintptr")
  234. if !found {
  235. t.Error("tuintptr was not found")
  236. }
  237. if x.(uintptr) != 3 {
  238. t.Error("tuintptr is not 3:", x)
  239. }
  240. }
  241. func TestIncrementWithUint8(t *testing.T) {
  242. tc := New(DefaultExpiration, 0)
  243. tc.Set("tuint8", uint8(1), DefaultExpiration)
  244. err := tc.Increment("tuint8", 2)
  245. if err != nil {
  246. t.Error("Error incrementing:", err)
  247. }
  248. x, found := tc.Get("tuint8")
  249. if !found {
  250. t.Error("tuint8 was not found")
  251. }
  252. if x.(uint8) != 3 {
  253. t.Error("tuint8 is not 3:", x)
  254. }
  255. }
  256. func TestIncrementWithUint16(t *testing.T) {
  257. tc := New(DefaultExpiration, 0)
  258. tc.Set("tuint16", uint16(1), DefaultExpiration)
  259. err := tc.Increment("tuint16", 2)
  260. if err != nil {
  261. t.Error("Error incrementing:", err)
  262. }
  263. x, found := tc.Get("tuint16")
  264. if !found {
  265. t.Error("tuint16 was not found")
  266. }
  267. if x.(uint16) != 3 {
  268. t.Error("tuint16 is not 3:", x)
  269. }
  270. }
  271. func TestIncrementWithUint32(t *testing.T) {
  272. tc := New(DefaultExpiration, 0)
  273. tc.Set("tuint32", uint32(1), DefaultExpiration)
  274. err := tc.Increment("tuint32", 2)
  275. if err != nil {
  276. t.Error("Error incrementing:", err)
  277. }
  278. x, found := tc.Get("tuint32")
  279. if !found {
  280. t.Error("tuint32 was not found")
  281. }
  282. if x.(uint32) != 3 {
  283. t.Error("tuint32 is not 3:", x)
  284. }
  285. }
  286. func TestIncrementWithUint64(t *testing.T) {
  287. tc := New(DefaultExpiration, 0)
  288. tc.Set("tuint64", uint64(1), DefaultExpiration)
  289. err := tc.Increment("tuint64", 2)
  290. if err != nil {
  291. t.Error("Error incrementing:", err)
  292. }
  293. x, found := tc.Get("tuint64")
  294. if !found {
  295. t.Error("tuint64 was not found")
  296. }
  297. if x.(uint64) != 3 {
  298. t.Error("tuint64 is not 3:", x)
  299. }
  300. }
  301. func TestIncrementWithFloat32(t *testing.T) {
  302. tc := New(DefaultExpiration, 0)
  303. tc.Set("float32", float32(1.5), DefaultExpiration)
  304. err := tc.Increment("float32", 2)
  305. if err != nil {
  306. t.Error("Error incrementing:", err)
  307. }
  308. x, found := tc.Get("float32")
  309. if !found {
  310. t.Error("float32 was not found")
  311. }
  312. if x.(float32) != 3.5 {
  313. t.Error("float32 is not 3.5:", x)
  314. }
  315. }
  316. func TestIncrementWithFloat64(t *testing.T) {
  317. tc := New(DefaultExpiration, 0)
  318. tc.Set("float64", float64(1.5), DefaultExpiration)
  319. err := tc.Increment("float64", 2)
  320. if err != nil {
  321. t.Error("Error incrementing:", err)
  322. }
  323. x, found := tc.Get("float64")
  324. if !found {
  325. t.Error("float64 was not found")
  326. }
  327. if x.(float64) != 3.5 {
  328. t.Error("float64 is not 3.5:", x)
  329. }
  330. }
  331. func TestIncrementFloatWithFloat32(t *testing.T) {
  332. tc := New(DefaultExpiration, 0)
  333. tc.Set("float32", float32(1.5), DefaultExpiration)
  334. err := tc.IncrementFloat("float32", 2)
  335. if err != nil {
  336. t.Error("Error incrementfloating:", err)
  337. }
  338. x, found := tc.Get("float32")
  339. if !found {
  340. t.Error("float32 was not found")
  341. }
  342. if x.(float32) != 3.5 {
  343. t.Error("float32 is not 3.5:", x)
  344. }
  345. }
  346. func TestIncrementFloatWithFloat64(t *testing.T) {
  347. tc := New(DefaultExpiration, 0)
  348. tc.Set("float64", float64(1.5), DefaultExpiration)
  349. err := tc.IncrementFloat("float64", 2)
  350. if err != nil {
  351. t.Error("Error incrementfloating:", err)
  352. }
  353. x, found := tc.Get("float64")
  354. if !found {
  355. t.Error("float64 was not found")
  356. }
  357. if x.(float64) != 3.5 {
  358. t.Error("float64 is not 3.5:", x)
  359. }
  360. }
  361. func TestDecrementWithInt(t *testing.T) {
  362. tc := New(DefaultExpiration, 0)
  363. tc.Set("int", int(5), DefaultExpiration)
  364. err := tc.Decrement("int", 2)
  365. if err != nil {
  366. t.Error("Error decrementing:", err)
  367. }
  368. x, found := tc.Get("int")
  369. if !found {
  370. t.Error("int was not found")
  371. }
  372. if x.(int) != 3 {
  373. t.Error("int is not 3:", x)
  374. }
  375. }
  376. func TestDecrementWithInt8(t *testing.T) {
  377. tc := New(DefaultExpiration, 0)
  378. tc.Set("int8", int8(5), DefaultExpiration)
  379. err := tc.Decrement("int8", 2)
  380. if err != nil {
  381. t.Error("Error decrementing:", err)
  382. }
  383. x, found := tc.Get("int8")
  384. if !found {
  385. t.Error("int8 was not found")
  386. }
  387. if x.(int8) != 3 {
  388. t.Error("int8 is not 3:", x)
  389. }
  390. }
  391. func TestDecrementWithInt16(t *testing.T) {
  392. tc := New(DefaultExpiration, 0)
  393. tc.Set("int16", int16(5), DefaultExpiration)
  394. err := tc.Decrement("int16", 2)
  395. if err != nil {
  396. t.Error("Error decrementing:", err)
  397. }
  398. x, found := tc.Get("int16")
  399. if !found {
  400. t.Error("int16 was not found")
  401. }
  402. if x.(int16) != 3 {
  403. t.Error("int16 is not 3:", x)
  404. }
  405. }
  406. func TestDecrementWithInt32(t *testing.T) {
  407. tc := New(DefaultExpiration, 0)
  408. tc.Set("int32", int32(5), DefaultExpiration)
  409. err := tc.Decrement("int32", 2)
  410. if err != nil {
  411. t.Error("Error decrementing:", err)
  412. }
  413. x, found := tc.Get("int32")
  414. if !found {
  415. t.Error("int32 was not found")
  416. }
  417. if x.(int32) != 3 {
  418. t.Error("int32 is not 3:", x)
  419. }
  420. }
  421. func TestDecrementWithInt64(t *testing.T) {
  422. tc := New(DefaultExpiration, 0)
  423. tc.Set("int64", int64(5), DefaultExpiration)
  424. err := tc.Decrement("int64", 2)
  425. if err != nil {
  426. t.Error("Error decrementing:", err)
  427. }
  428. x, found := tc.Get("int64")
  429. if !found {
  430. t.Error("int64 was not found")
  431. }
  432. if x.(int64) != 3 {
  433. t.Error("int64 is not 3:", x)
  434. }
  435. }
  436. func TestDecrementWithUint(t *testing.T) {
  437. tc := New(DefaultExpiration, 0)
  438. tc.Set("uint", uint(5), DefaultExpiration)
  439. err := tc.Decrement("uint", 2)
  440. if err != nil {
  441. t.Error("Error decrementing:", err)
  442. }
  443. x, found := tc.Get("uint")
  444. if !found {
  445. t.Error("uint was not found")
  446. }
  447. if x.(uint) != 3 {
  448. t.Error("uint is not 3:", x)
  449. }
  450. }
  451. func TestDecrementWithUintptr(t *testing.T) {
  452. tc := New(DefaultExpiration, 0)
  453. tc.Set("uintptr", uintptr(5), DefaultExpiration)
  454. err := tc.Decrement("uintptr", 2)
  455. if err != nil {
  456. t.Error("Error decrementing:", err)
  457. }
  458. x, found := tc.Get("uintptr")
  459. if !found {
  460. t.Error("uintptr was not found")
  461. }
  462. if x.(uintptr) != 3 {
  463. t.Error("uintptr is not 3:", x)
  464. }
  465. }
  466. func TestDecrementWithUint8(t *testing.T) {
  467. tc := New(DefaultExpiration, 0)
  468. tc.Set("uint8", uint8(5), DefaultExpiration)
  469. err := tc.Decrement("uint8", 2)
  470. if err != nil {
  471. t.Error("Error decrementing:", err)
  472. }
  473. x, found := tc.Get("uint8")
  474. if !found {
  475. t.Error("uint8 was not found")
  476. }
  477. if x.(uint8) != 3 {
  478. t.Error("uint8 is not 3:", x)
  479. }
  480. }
  481. func TestDecrementWithUint16(t *testing.T) {
  482. tc := New(DefaultExpiration, 0)
  483. tc.Set("uint16", uint16(5), DefaultExpiration)
  484. err := tc.Decrement("uint16", 2)
  485. if err != nil {
  486. t.Error("Error decrementing:", err)
  487. }
  488. x, found := tc.Get("uint16")
  489. if !found {
  490. t.Error("uint16 was not found")
  491. }
  492. if x.(uint16) != 3 {
  493. t.Error("uint16 is not 3:", x)
  494. }
  495. }
  496. func TestDecrementWithUint32(t *testing.T) {
  497. tc := New(DefaultExpiration, 0)
  498. tc.Set("uint32", uint32(5), DefaultExpiration)
  499. err := tc.Decrement("uint32", 2)
  500. if err != nil {
  501. t.Error("Error decrementing:", err)
  502. }
  503. x, found := tc.Get("uint32")
  504. if !found {
  505. t.Error("uint32 was not found")
  506. }
  507. if x.(uint32) != 3 {
  508. t.Error("uint32 is not 3:", x)
  509. }
  510. }
  511. func TestDecrementWithUint64(t *testing.T) {
  512. tc := New(DefaultExpiration, 0)
  513. tc.Set("uint64", uint64(5), DefaultExpiration)
  514. err := tc.Decrement("uint64", 2)
  515. if err != nil {
  516. t.Error("Error decrementing:", err)
  517. }
  518. x, found := tc.Get("uint64")
  519. if !found {
  520. t.Error("uint64 was not found")
  521. }
  522. if x.(uint64) != 3 {
  523. t.Error("uint64 is not 3:", x)
  524. }
  525. }
  526. func TestDecrementWithFloat32(t *testing.T) {
  527. tc := New(DefaultExpiration, 0)
  528. tc.Set("float32", float32(5.5), DefaultExpiration)
  529. err := tc.Decrement("float32", 2)
  530. if err != nil {
  531. t.Error("Error decrementing:", err)
  532. }
  533. x, found := tc.Get("float32")
  534. if !found {
  535. t.Error("float32 was not found")
  536. }
  537. if x.(float32) != 3.5 {
  538. t.Error("float32 is not 3:", x)
  539. }
  540. }
  541. func TestDecrementWithFloat64(t *testing.T) {
  542. tc := New(DefaultExpiration, 0)
  543. tc.Set("float64", float64(5.5), DefaultExpiration)
  544. err := tc.Decrement("float64", 2)
  545. if err != nil {
  546. t.Error("Error decrementing:", err)
  547. }
  548. x, found := tc.Get("float64")
  549. if !found {
  550. t.Error("float64 was not found")
  551. }
  552. if x.(float64) != 3.5 {
  553. t.Error("float64 is not 3:", x)
  554. }
  555. }
  556. func TestDecrementFloatWithFloat32(t *testing.T) {
  557. tc := New(DefaultExpiration, 0)
  558. tc.Set("float32", float32(5.5), DefaultExpiration)
  559. err := tc.DecrementFloat("float32", 2)
  560. if err != nil {
  561. t.Error("Error decrementing:", err)
  562. }
  563. x, found := tc.Get("float32")
  564. if !found {
  565. t.Error("float32 was not found")
  566. }
  567. if x.(float32) != 3.5 {
  568. t.Error("float32 is not 3:", x)
  569. }
  570. }
  571. func TestDecrementFloatWithFloat64(t *testing.T) {
  572. tc := New(DefaultExpiration, 0)
  573. tc.Set("float64", float64(5.5), DefaultExpiration)
  574. err := tc.DecrementFloat("float64", 2)
  575. if err != nil {
  576. t.Error("Error decrementing:", err)
  577. }
  578. x, found := tc.Get("float64")
  579. if !found {
  580. t.Error("float64 was not found")
  581. }
  582. if x.(float64) != 3.5 {
  583. t.Error("float64 is not 3:", x)
  584. }
  585. }
  586. func TestIncrementInt(t *testing.T) {
  587. tc := New(DefaultExpiration, 0)
  588. tc.Set("tint", 1, DefaultExpiration)
  589. n, err := tc.IncrementInt("tint", 2)
  590. if err != nil {
  591. t.Error("Error incrementing:", err)
  592. }
  593. if n != 3 {
  594. t.Error("Returned number is not 3:", n)
  595. }
  596. x, found := tc.Get("tint")
  597. if !found {
  598. t.Error("tint was not found")
  599. }
  600. if x.(int) != 3 {
  601. t.Error("tint is not 3:", x)
  602. }
  603. }
  604. func TestIncrementInt8(t *testing.T) {
  605. tc := New(DefaultExpiration, 0)
  606. tc.Set("tint8", int8(1), DefaultExpiration)
  607. n, err := tc.IncrementInt8("tint8", 2)
  608. if err != nil {
  609. t.Error("Error incrementing:", err)
  610. }
  611. if n != 3 {
  612. t.Error("Returned number is not 3:", n)
  613. }
  614. x, found := tc.Get("tint8")
  615. if !found {
  616. t.Error("tint8 was not found")
  617. }
  618. if x.(int8) != 3 {
  619. t.Error("tint8 is not 3:", x)
  620. }
  621. }
  622. func TestIncrementInt16(t *testing.T) {
  623. tc := New(DefaultExpiration, 0)
  624. tc.Set("tint16", int16(1), DefaultExpiration)
  625. n, err := tc.IncrementInt16("tint16", 2)
  626. if err != nil {
  627. t.Error("Error incrementing:", err)
  628. }
  629. if n != 3 {
  630. t.Error("Returned number is not 3:", n)
  631. }
  632. x, found := tc.Get("tint16")
  633. if !found {
  634. t.Error("tint16 was not found")
  635. }
  636. if x.(int16) != 3 {
  637. t.Error("tint16 is not 3:", x)
  638. }
  639. }
  640. func TestIncrementInt32(t *testing.T) {
  641. tc := New(DefaultExpiration, 0)
  642. tc.Set("tint32", int32(1), DefaultExpiration)
  643. n, err := tc.IncrementInt32("tint32", 2)
  644. if err != nil {
  645. t.Error("Error incrementing:", err)
  646. }
  647. if n != 3 {
  648. t.Error("Returned number is not 3:", n)
  649. }
  650. x, found := tc.Get("tint32")
  651. if !found {
  652. t.Error("tint32 was not found")
  653. }
  654. if x.(int32) != 3 {
  655. t.Error("tint32 is not 3:", x)
  656. }
  657. }
  658. func TestIncrementInt64(t *testing.T) {
  659. tc := New(DefaultExpiration, 0)
  660. tc.Set("tint64", int64(1), DefaultExpiration)
  661. n, err := tc.IncrementInt64("tint64", 2)
  662. if err != nil {
  663. t.Error("Error incrementing:", err)
  664. }
  665. if n != 3 {
  666. t.Error("Returned number is not 3:", n)
  667. }
  668. x, found := tc.Get("tint64")
  669. if !found {
  670. t.Error("tint64 was not found")
  671. }
  672. if x.(int64) != 3 {
  673. t.Error("tint64 is not 3:", x)
  674. }
  675. }
  676. func TestIncrementUint(t *testing.T) {
  677. tc := New(DefaultExpiration, 0)
  678. tc.Set("tuint", uint(1), DefaultExpiration)
  679. n, err := tc.IncrementUint("tuint", 2)
  680. if err != nil {
  681. t.Error("Error incrementing:", err)
  682. }
  683. if n != 3 {
  684. t.Error("Returned number is not 3:", n)
  685. }
  686. x, found := tc.Get("tuint")
  687. if !found {
  688. t.Error("tuint was not found")
  689. }
  690. if x.(uint) != 3 {
  691. t.Error("tuint is not 3:", x)
  692. }
  693. }
  694. func TestIncrementUintptr(t *testing.T) {
  695. tc := New(DefaultExpiration, 0)
  696. tc.Set("tuintptr", uintptr(1), DefaultExpiration)
  697. n, err := tc.IncrementUintptr("tuintptr", 2)
  698. if err != nil {
  699. t.Error("Error incrementing:", err)
  700. }
  701. if n != 3 {
  702. t.Error("Returned number is not 3:", n)
  703. }
  704. x, found := tc.Get("tuintptr")
  705. if !found {
  706. t.Error("tuintptr was not found")
  707. }
  708. if x.(uintptr) != 3 {
  709. t.Error("tuintptr is not 3:", x)
  710. }
  711. }
  712. func TestIncrementUint8(t *testing.T) {
  713. tc := New(DefaultExpiration, 0)
  714. tc.Set("tuint8", uint8(1), DefaultExpiration)
  715. n, err := tc.IncrementUint8("tuint8", 2)
  716. if err != nil {
  717. t.Error("Error incrementing:", err)
  718. }
  719. if n != 3 {
  720. t.Error("Returned number is not 3:", n)
  721. }
  722. x, found := tc.Get("tuint8")
  723. if !found {
  724. t.Error("tuint8 was not found")
  725. }
  726. if x.(uint8) != 3 {
  727. t.Error("tuint8 is not 3:", x)
  728. }
  729. }
  730. func TestIncrementUint16(t *testing.T) {
  731. tc := New(DefaultExpiration, 0)
  732. tc.Set("tuint16", uint16(1), DefaultExpiration)
  733. n, err := tc.IncrementUint16("tuint16", 2)
  734. if err != nil {
  735. t.Error("Error incrementing:", err)
  736. }
  737. if n != 3 {
  738. t.Error("Returned number is not 3:", n)
  739. }
  740. x, found := tc.Get("tuint16")
  741. if !found {
  742. t.Error("tuint16 was not found")
  743. }
  744. if x.(uint16) != 3 {
  745. t.Error("tuint16 is not 3:", x)
  746. }
  747. }
  748. func TestIncrementUint32(t *testing.T) {
  749. tc := New(DefaultExpiration, 0)
  750. tc.Set("tuint32", uint32(1), DefaultExpiration)
  751. n, err := tc.IncrementUint32("tuint32", 2)
  752. if err != nil {
  753. t.Error("Error incrementing:", err)
  754. }
  755. if n != 3 {
  756. t.Error("Returned number is not 3:", n)
  757. }
  758. x, found := tc.Get("tuint32")
  759. if !found {
  760. t.Error("tuint32 was not found")
  761. }
  762. if x.(uint32) != 3 {
  763. t.Error("tuint32 is not 3:", x)
  764. }
  765. }
  766. func TestIncrementUint64(t *testing.T) {
  767. tc := New(DefaultExpiration, 0)
  768. tc.Set("tuint64", uint64(1), DefaultExpiration)
  769. n, err := tc.IncrementUint64("tuint64", 2)
  770. if err != nil {
  771. t.Error("Error incrementing:", err)
  772. }
  773. if n != 3 {
  774. t.Error("Returned number is not 3:", n)
  775. }
  776. x, found := tc.Get("tuint64")
  777. if !found {
  778. t.Error("tuint64 was not found")
  779. }
  780. if x.(uint64) != 3 {
  781. t.Error("tuint64 is not 3:", x)
  782. }
  783. }
  784. func TestIncrementFloat32(t *testing.T) {
  785. tc := New(DefaultExpiration, 0)
  786. tc.Set("float32", float32(1.5), DefaultExpiration)
  787. n, err := tc.IncrementFloat32("float32", 2)
  788. if err != nil {
  789. t.Error("Error incrementing:", err)
  790. }
  791. if n != 3.5 {
  792. t.Error("Returned number is not 3.5:", n)
  793. }
  794. x, found := tc.Get("float32")
  795. if !found {
  796. t.Error("float32 was not found")
  797. }
  798. if x.(float32) != 3.5 {
  799. t.Error("float32 is not 3.5:", x)
  800. }
  801. }
  802. func TestIncrementFloat64(t *testing.T) {
  803. tc := New(DefaultExpiration, 0)
  804. tc.Set("float64", float64(1.5), DefaultExpiration)
  805. n, err := tc.IncrementFloat64("float64", 2)
  806. if err != nil {
  807. t.Error("Error incrementing:", err)
  808. }
  809. if n != 3.5 {
  810. t.Error("Returned number is not 3.5:", n)
  811. }
  812. x, found := tc.Get("float64")
  813. if !found {
  814. t.Error("float64 was not found")
  815. }
  816. if x.(float64) != 3.5 {
  817. t.Error("float64 is not 3.5:", x)
  818. }
  819. }
  820. func TestDecrementInt8(t *testing.T) {
  821. tc := New(DefaultExpiration, 0)
  822. tc.Set("int8", int8(5), DefaultExpiration)
  823. n, err := tc.DecrementInt8("int8", 2)
  824. if err != nil {
  825. t.Error("Error decrementing:", err)
  826. }
  827. if n != 3 {
  828. t.Error("Returned number is not 3:", n)
  829. }
  830. x, found := tc.Get("int8")
  831. if !found {
  832. t.Error("int8 was not found")
  833. }
  834. if x.(int8) != 3 {
  835. t.Error("int8 is not 3:", x)
  836. }
  837. }
  838. func TestDecrementInt16(t *testing.T) {
  839. tc := New(DefaultExpiration, 0)
  840. tc.Set("int16", int16(5), DefaultExpiration)
  841. n, err := tc.DecrementInt16("int16", 2)
  842. if err != nil {
  843. t.Error("Error decrementing:", err)
  844. }
  845. if n != 3 {
  846. t.Error("Returned number is not 3:", n)
  847. }
  848. x, found := tc.Get("int16")
  849. if !found {
  850. t.Error("int16 was not found")
  851. }
  852. if x.(int16) != 3 {
  853. t.Error("int16 is not 3:", x)
  854. }
  855. }
  856. func TestDecrementInt32(t *testing.T) {
  857. tc := New(DefaultExpiration, 0)
  858. tc.Set("int32", int32(5), DefaultExpiration)
  859. n, err := tc.DecrementInt32("int32", 2)
  860. if err != nil {
  861. t.Error("Error decrementing:", err)
  862. }
  863. if n != 3 {
  864. t.Error("Returned number is not 3:", n)
  865. }
  866. x, found := tc.Get("int32")
  867. if !found {
  868. t.Error("int32 was not found")
  869. }
  870. if x.(int32) != 3 {
  871. t.Error("int32 is not 3:", x)
  872. }
  873. }
  874. func TestDecrementInt64(t *testing.T) {
  875. tc := New(DefaultExpiration, 0)
  876. tc.Set("int64", int64(5), DefaultExpiration)
  877. n, err := tc.DecrementInt64("int64", 2)
  878. if err != nil {
  879. t.Error("Error decrementing:", err)
  880. }
  881. if n != 3 {
  882. t.Error("Returned number is not 3:", n)
  883. }
  884. x, found := tc.Get("int64")
  885. if !found {
  886. t.Error("int64 was not found")
  887. }
  888. if x.(int64) != 3 {
  889. t.Error("int64 is not 3:", x)
  890. }
  891. }
  892. func TestDecrementUint(t *testing.T) {
  893. tc := New(DefaultExpiration, 0)
  894. tc.Set("uint", uint(5), DefaultExpiration)
  895. n, err := tc.DecrementUint("uint", 2)
  896. if err != nil {
  897. t.Error("Error decrementing:", err)
  898. }
  899. if n != 3 {
  900. t.Error("Returned number is not 3:", n)
  901. }
  902. x, found := tc.Get("uint")
  903. if !found {
  904. t.Error("uint was not found")
  905. }
  906. if x.(uint) != 3 {
  907. t.Error("uint is not 3:", x)
  908. }
  909. }
  910. func TestDecrementUintptr(t *testing.T) {
  911. tc := New(DefaultExpiration, 0)
  912. tc.Set("uintptr", uintptr(5), DefaultExpiration)
  913. n, err := tc.DecrementUintptr("uintptr", 2)
  914. if err != nil {
  915. t.Error("Error decrementing:", err)
  916. }
  917. if n != 3 {
  918. t.Error("Returned number is not 3:", n)
  919. }
  920. x, found := tc.Get("uintptr")
  921. if !found {
  922. t.Error("uintptr was not found")
  923. }
  924. if x.(uintptr) != 3 {
  925. t.Error("uintptr is not 3:", x)
  926. }
  927. }
  928. func TestDecrementUint8(t *testing.T) {
  929. tc := New(DefaultExpiration, 0)
  930. tc.Set("uint8", uint8(5), DefaultExpiration)
  931. n, err := tc.DecrementUint8("uint8", 2)
  932. if err != nil {
  933. t.Error("Error decrementing:", err)
  934. }
  935. if n != 3 {
  936. t.Error("Returned number is not 3:", n)
  937. }
  938. x, found := tc.Get("uint8")
  939. if !found {
  940. t.Error("uint8 was not found")
  941. }
  942. if x.(uint8) != 3 {
  943. t.Error("uint8 is not 3:", x)
  944. }
  945. }
  946. func TestDecrementUint16(t *testing.T) {
  947. tc := New(DefaultExpiration, 0)
  948. tc.Set("uint16", uint16(5), DefaultExpiration)
  949. n, err := tc.DecrementUint16("uint16", 2)
  950. if err != nil {
  951. t.Error("Error decrementing:", err)
  952. }
  953. if n != 3 {
  954. t.Error("Returned number is not 3:", n)
  955. }
  956. x, found := tc.Get("uint16")
  957. if !found {
  958. t.Error("uint16 was not found")
  959. }
  960. if x.(uint16) != 3 {
  961. t.Error("uint16 is not 3:", x)
  962. }
  963. }
  964. func TestDecrementUint32(t *testing.T) {
  965. tc := New(DefaultExpiration, 0)
  966. tc.Set("uint32", uint32(5), DefaultExpiration)
  967. n, err := tc.DecrementUint32("uint32", 2)
  968. if err != nil {
  969. t.Error("Error decrementing:", err)
  970. }
  971. if n != 3 {
  972. t.Error("Returned number is not 3:", n)
  973. }
  974. x, found := tc.Get("uint32")
  975. if !found {
  976. t.Error("uint32 was not found")
  977. }
  978. if x.(uint32) != 3 {
  979. t.Error("uint32 is not 3:", x)
  980. }
  981. }
  982. func TestDecrementUint64(t *testing.T) {
  983. tc := New(DefaultExpiration, 0)
  984. tc.Set("uint64", uint64(5), DefaultExpiration)
  985. n, err := tc.DecrementUint64("uint64", 2)
  986. if err != nil {
  987. t.Error("Error decrementing:", err)
  988. }
  989. if n != 3 {
  990. t.Error("Returned number is not 3:", n)
  991. }
  992. x, found := tc.Get("uint64")
  993. if !found {
  994. t.Error("uint64 was not found")
  995. }
  996. if x.(uint64) != 3 {
  997. t.Error("uint64 is not 3:", x)
  998. }
  999. }
  1000. func TestDecrementFloat32(t *testing.T) {
  1001. tc := New(DefaultExpiration, 0)
  1002. tc.Set("float32", float32(5), DefaultExpiration)
  1003. n, err := tc.DecrementFloat32("float32", 2)
  1004. if err != nil {
  1005. t.Error("Error decrementing:", err)
  1006. }
  1007. if n != 3 {
  1008. t.Error("Returned number is not 3:", n)
  1009. }
  1010. x, found := tc.Get("float32")
  1011. if !found {
  1012. t.Error("float32 was not found")
  1013. }
  1014. if x.(float32) != 3 {
  1015. t.Error("float32 is not 3:", x)
  1016. }
  1017. }
  1018. func TestDecrementFloat64(t *testing.T) {
  1019. tc := New(DefaultExpiration, 0)
  1020. tc.Set("float64", float64(5), DefaultExpiration)
  1021. n, err := tc.DecrementFloat64("float64", 2)
  1022. if err != nil {
  1023. t.Error("Error decrementing:", err)
  1024. }
  1025. if n != 3 {
  1026. t.Error("Returned number is not 3:", n)
  1027. }
  1028. x, found := tc.Get("float64")
  1029. if !found {
  1030. t.Error("float64 was not found")
  1031. }
  1032. if x.(float64) != 3 {
  1033. t.Error("float64 is not 3:", x)
  1034. }
  1035. }
  1036. func TestAdd(t *testing.T) {
  1037. tc := New(DefaultExpiration, 0)
  1038. err := tc.Add("foo", "bar", DefaultExpiration)
  1039. if err != nil {
  1040. t.Error("Couldn't add foo even though it shouldn't exist")
  1041. }
  1042. err = tc.Add("foo", "baz", DefaultExpiration)
  1043. if err == nil {
  1044. t.Error("Successfully added another foo when it should have returned an error")
  1045. }
  1046. }
  1047. func TestReplace(t *testing.T) {
  1048. tc := New(DefaultExpiration, 0)
  1049. err := tc.Replace("foo", "bar", DefaultExpiration)
  1050. if err == nil {
  1051. t.Error("Replaced foo when it shouldn't exist")
  1052. }
  1053. tc.Set("foo", "bar", DefaultExpiration)
  1054. err = tc.Replace("foo", "bar", DefaultExpiration)
  1055. if err != nil {
  1056. t.Error("Couldn't replace existing key foo")
  1057. }
  1058. }
  1059. func TestDelete(t *testing.T) {
  1060. tc := New(DefaultExpiration, 0)
  1061. tc.Set("foo", "bar", DefaultExpiration)
  1062. tc.Delete("foo")
  1063. x, found := tc.Get("foo")
  1064. if found {
  1065. t.Error("foo was found, but it should have been deleted")
  1066. }
  1067. if x != nil {
  1068. t.Error("x is not nil:", x)
  1069. }
  1070. }
  1071. func TestItemCount(t *testing.T) {
  1072. tc := New(DefaultExpiration, 0)
  1073. tc.Set("foo", "1", DefaultExpiration)
  1074. tc.Set("bar", "2", DefaultExpiration)
  1075. tc.Set("baz", "3", DefaultExpiration)
  1076. if n := tc.ItemCount(); n != 3 {
  1077. t.Errorf("Item count is not 3: %d", n)
  1078. }
  1079. }
  1080. func TestFlush(t *testing.T) {
  1081. tc := New(DefaultExpiration, 0)
  1082. tc.Set("foo", "bar", DefaultExpiration)
  1083. tc.Set("baz", "yes", DefaultExpiration)
  1084. tc.Flush()
  1085. x, found := tc.Get("foo")
  1086. if found {
  1087. t.Error("foo was found, but it should have been deleted")
  1088. }
  1089. if x != nil {
  1090. t.Error("x is not nil:", x)
  1091. }
  1092. x, found = tc.Get("baz")
  1093. if found {
  1094. t.Error("baz was found, but it should have been deleted")
  1095. }
  1096. if x != nil {
  1097. t.Error("x is not nil:", x)
  1098. }
  1099. }
  1100. func TestIncrementOverflowInt(t *testing.T) {
  1101. tc := New(DefaultExpiration, 0)
  1102. tc.Set("int8", int8(127), DefaultExpiration)
  1103. err := tc.Increment("int8", 1)
  1104. if err != nil {
  1105. t.Error("Error incrementing int8:", err)
  1106. }
  1107. x, _ := tc.Get("int8")
  1108. int8 := x.(int8)
  1109. if int8 != -128 {
  1110. t.Error("int8 did not overflow as expected; value:", int8)
  1111. }
  1112. }
  1113. func TestIncrementOverflowUint(t *testing.T) {
  1114. tc := New(DefaultExpiration, 0)
  1115. tc.Set("uint8", uint8(255), DefaultExpiration)
  1116. err := tc.Increment("uint8", 1)
  1117. if err != nil {
  1118. t.Error("Error incrementing int8:", err)
  1119. }
  1120. x, _ := tc.Get("uint8")
  1121. uint8 := x.(uint8)
  1122. if uint8 != 0 {
  1123. t.Error("uint8 did not overflow as expected; value:", uint8)
  1124. }
  1125. }
  1126. func TestDecrementUnderflowUint(t *testing.T) {
  1127. tc := New(DefaultExpiration, 0)
  1128. tc.Set("uint8", uint8(0), DefaultExpiration)
  1129. err := tc.Decrement("uint8", 1)
  1130. if err != nil {
  1131. t.Error("Error decrementing int8:", err)
  1132. }
  1133. x, _ := tc.Get("uint8")
  1134. uint8 := x.(uint8)
  1135. if uint8 != 255 {
  1136. t.Error("uint8 did not underflow as expected; value:", uint8)
  1137. }
  1138. }
  1139. // TODO: Ring buffer is more efficient but doesn't guarantee that the actually
  1140. // oldest items are removed, just some old items. This shouldn't be significant
  1141. // for large caches, but we can't test it easily.
  1142. //
  1143. // func TestDeleteLRU(t *testing.T) {
  1144. // tc := NewWithLRU(1*time.Second, 0, 1)
  1145. // tc.Set("foo", 0, DefaultExpiration)
  1146. // tc.Set("bar", 1, DefaultExpiration)
  1147. // tc.Set("baz", 2, DefaultExpiration)
  1148. // tc.Get("foo")
  1149. // tc.Get("baz")
  1150. // time.Sleep(5 * time.Millisecond)
  1151. // tc.Get("bar")
  1152. // // Bar was accessed most recently, and should be the only value that
  1153. // // stays.
  1154. // tc.DeleteLRU()
  1155. // if tc.ItemCount() != 1 {
  1156. // t.Error("tc.ItemCount() is not 1")
  1157. // }
  1158. // if _, found := tc.Get("bar"); !found {
  1159. // t.Error("bar was not found")
  1160. // }
  1161. // }
  1162. func TestDeleteLRU(t *testing.T) {
  1163. tc := NewWithLRU(1*time.Second, 0, 1)
  1164. tc.Set("foo", 0, DefaultExpiration)
  1165. tc.Set("bar", 1, DefaultExpiration)
  1166. tc.Set("baz", 2, DefaultExpiration)
  1167. tc.DeleteLRU()
  1168. if tc.ItemCount() != 1 {
  1169. t.Error("tc.ItemCount() is not 1")
  1170. }
  1171. }
  1172. func TestOnEvicted(t *testing.T) {
  1173. tc := New(DefaultExpiration, 0)
  1174. tc.Set("foo", 3, DefaultExpiration)
  1175. if tc.onEvicted != nil {
  1176. t.Fatal("tc.onEvicted is not nil")
  1177. }
  1178. works := false
  1179. tc.OnEvicted(func(k string, v interface{}) {
  1180. if k == "foo" && v.(int) == 3 {
  1181. works = true
  1182. }
  1183. tc.Set("bar", 4, DefaultExpiration)
  1184. })
  1185. tc.Delete("foo")
  1186. x, _ := tc.Get("bar")
  1187. if !works {
  1188. t.Error("works bool not true")
  1189. }
  1190. if x.(int) != 4 {
  1191. t.Error("bar was not 4")
  1192. }
  1193. }
  1194. func TestCacheSerialization(t *testing.T) {
  1195. tc := New(DefaultExpiration, 0)
  1196. testFillAndSerialize(t, tc)
  1197. // Check if gob.Register behaves properly even after multiple gob.Register
  1198. // on c.Items (many of which will be the same type)
  1199. testFillAndSerialize(t, tc)
  1200. }
  1201. func testFillAndSerialize(t *testing.T, tc *Cache) {
  1202. tc.Set("a", "a", DefaultExpiration)
  1203. tc.Set("b", "b", DefaultExpiration)
  1204. tc.Set("c", "c", DefaultExpiration)
  1205. tc.Set("expired", "foo", 1*time.Millisecond)
  1206. tc.Set("*struct", &TestStruct{Num: 1}, DefaultExpiration)
  1207. tc.Set("[]struct", []TestStruct{
  1208. {Num: 2},
  1209. {Num: 3},
  1210. }, DefaultExpiration)
  1211. tc.Set("[]*struct", []*TestStruct{
  1212. {Num: 4},
  1213. {Num: 5},
  1214. }, DefaultExpiration)
  1215. tc.Set("structception", &TestStruct{
  1216. Num: 42,
  1217. Children: []*TestStruct{
  1218. {Num: 6174},
  1219. {Num: 4716},
  1220. },
  1221. }, DefaultExpiration)
  1222. fp := &bytes.Buffer{}
  1223. err := tc.Save(fp)
  1224. if err != nil {
  1225. t.Fatal("Couldn't save cache to fp:", err)
  1226. }
  1227. oc := New(DefaultExpiration, 0)
  1228. err = oc.Load(fp)
  1229. if err != nil {
  1230. t.Fatal("Couldn't load cache from fp:", err)
  1231. }
  1232. a, found := oc.Get("a")
  1233. if !found {
  1234. t.Error("a was not found")
  1235. }
  1236. if a.(string) != "a" {
  1237. t.Error("a is not a")
  1238. }
  1239. b, found := oc.Get("b")
  1240. if !found {
  1241. t.Error("b was not found")
  1242. }
  1243. if b.(string) != "b" {
  1244. t.Error("b is not b")
  1245. }
  1246. c, found := oc.Get("c")
  1247. if !found {
  1248. t.Error("c was not found")
  1249. }
  1250. if c.(string) != "c" {
  1251. t.Error("c is not c")
  1252. }
  1253. <-time.After(5 * time.Millisecond)
  1254. _, found = oc.Get("expired")
  1255. if found {
  1256. t.Error("expired was found")
  1257. }
  1258. s1, found := oc.Get("*struct")
  1259. if !found {
  1260. t.Error("*struct was not found")
  1261. }
  1262. if s1.(*TestStruct).Num != 1 {
  1263. t.Error("*struct.Num is not 1")
  1264. }
  1265. s2, found := oc.Get("[]struct")
  1266. if !found {
  1267. t.Error("[]struct was not found")
  1268. }
  1269. s2r := s2.([]TestStruct)
  1270. if len(s2r) != 2 {
  1271. t.Error("Length of s2r is not 2")
  1272. }
  1273. if s2r[0].Num != 2 {
  1274. t.Error("s2r[0].Num is not 2")
  1275. }
  1276. if s2r[1].Num != 3 {
  1277. t.Error("s2r[1].Num is not 3")
  1278. }
  1279. s3, found := oc.get("[]*struct")
  1280. if !found {
  1281. t.Error("[]*struct was not found")
  1282. }
  1283. s3r := s3.([]*TestStruct)
  1284. if len(s3r) != 2 {
  1285. t.Error("Length of s3r is not 2")
  1286. }
  1287. if s3r[0].Num != 4 {
  1288. t.Error("s3r[0].Num is not 4")
  1289. }
  1290. if s3r[1].Num != 5 {
  1291. t.Error("s3r[1].Num is not 5")
  1292. }
  1293. s4, found := oc.get("structception")
  1294. if !found {
  1295. t.Error("structception was not found")
  1296. }
  1297. s4r := s4.(*TestStruct)
  1298. if len(s4r.Children) != 2 {
  1299. t.Error("Length of s4r.Children is not 2")
  1300. }
  1301. if s4r.Children[0].Num != 6174 {
  1302. t.Error("s4r.Children[0].Num is not 6174")
  1303. }
  1304. if s4r.Children[1].Num != 4716 {
  1305. t.Error("s4r.Children[1].Num is not 4716")
  1306. }
  1307. }
  1308. func TestFileSerialization(t *testing.T) {
  1309. tc := New(DefaultExpiration, 0)
  1310. tc.Add("a", "a", DefaultExpiration)
  1311. tc.Add("b", "b", DefaultExpiration)
  1312. f, err := ioutil.TempFile("", "go-cache-cache.dat")
  1313. if err != nil {
  1314. t.Fatal("Couldn't create cache file:", err)
  1315. }
  1316. fname := f.Name()
  1317. f.Close()
  1318. tc.SaveFile(fname)
  1319. oc := New(DefaultExpiration, 0)
  1320. oc.Add("a", "aa", 0) // this should not be overwritten
  1321. err = oc.LoadFile(fname)
  1322. if err != nil {
  1323. t.Error(err)
  1324. }
  1325. a, found := oc.Get("a")
  1326. if !found {
  1327. t.Error("a was not found")
  1328. }
  1329. astr := a.(string)
  1330. if astr != "aa" {
  1331. if astr == "a" {
  1332. t.Error("a was overwritten")
  1333. } else {
  1334. t.Error("a is not aa")
  1335. }
  1336. }
  1337. b, found := oc.Get("b")
  1338. if !found {
  1339. t.Error("b was not found")
  1340. }
  1341. if b.(string) != "b" {
  1342. t.Error("b is not b")
  1343. }
  1344. }
  1345. func TestSerializeUnserializable(t *testing.T) {
  1346. tc := New(DefaultExpiration, 0)
  1347. ch := make(chan bool, 1)
  1348. ch <- true
  1349. tc.Set("chan", ch, DefaultExpiration)
  1350. fp := &bytes.Buffer{}
  1351. err := tc.Save(fp) // this should fail gracefully
  1352. if err.Error() != "gob NewTypeObject can't handle type: chan bool" {
  1353. t.Error("Error from Save was not gob NewTypeObject can't handle type chan bool:", err)
  1354. }
  1355. }
  1356. func BenchmarkCacheGetExpiring(b *testing.B) {
  1357. benchmarkCacheGet(b, 5*time.Minute)
  1358. }
  1359. func BenchmarkCacheGetNotExpiring(b *testing.B) {
  1360. benchmarkCacheGet(b, NoExpiration)
  1361. }
  1362. func benchmarkCacheGet(b *testing.B, exp time.Duration) {
  1363. b.StopTimer()
  1364. tc := New(exp, 0)
  1365. tc.Set("foo", "bar", DefaultExpiration)
  1366. b.StartTimer()
  1367. for i := 0; i < b.N; i++ {
  1368. tc.Get("foo")
  1369. }
  1370. }
  1371. func BenchmarkCacheWithLRUGetExpiring(b *testing.B) {
  1372. benchmarkCacheWithLRUGet(b, 5*time.Minute, 10)
  1373. }
  1374. func BenchmarkCacheWithLRUGetNotExpiring(b *testing.B) {
  1375. benchmarkCacheWithLRUGet(b, NoExpiration, 10)
  1376. }
  1377. func benchmarkCacheWithLRUGet(b *testing.B, exp time.Duration, max int) {
  1378. b.StopTimer()
  1379. tc := NewWithLRU(exp, 0, max)
  1380. tc.Set("foo", "bar", DefaultExpiration)
  1381. b.StartTimer()
  1382. for i := 0; i < b.N; i++ {
  1383. tc.Get("foo")
  1384. }
  1385. }
  1386. func BenchmarkRWMutexMapGet(b *testing.B) {
  1387. b.StopTimer()
  1388. m := map[string]string{
  1389. "foo": "bar",
  1390. }
  1391. mu := sync.RWMutex{}
  1392. b.StartTimer()
  1393. for i := 0; i < b.N; i++ {
  1394. mu.RLock()
  1395. _, _ = m["foo"]
  1396. mu.RUnlock()
  1397. }
  1398. }
  1399. func BenchmarkRWMutexInterfaceMapGetStruct(b *testing.B) {
  1400. b.StopTimer()
  1401. s := struct{ name string }{name: "foo"}
  1402. m := map[interface{}]string{
  1403. s: "bar",
  1404. }
  1405. mu := sync.RWMutex{}
  1406. b.StartTimer()
  1407. for i := 0; i < b.N; i++ {
  1408. mu.RLock()
  1409. _, _ = m[s]
  1410. mu.RUnlock()
  1411. }
  1412. }
  1413. func BenchmarkRWMutexInterfaceMapGetString(b *testing.B) {
  1414. b.StopTimer()
  1415. m := map[interface{}]string{
  1416. "foo": "bar",
  1417. }
  1418. mu := sync.RWMutex{}
  1419. b.StartTimer()
  1420. for i := 0; i < b.N; i++ {
  1421. mu.RLock()
  1422. _, _ = m["foo"]
  1423. mu.RUnlock()
  1424. }
  1425. }
  1426. func BenchmarkCacheGetConcurrentExpiring(b *testing.B) {
  1427. benchmarkCacheGetConcurrent(b, 5*time.Minute)
  1428. }
  1429. func BenchmarkCacheGetConcurrentNotExpiring(b *testing.B) {
  1430. benchmarkCacheGetConcurrent(b, NoExpiration)
  1431. }
  1432. func benchmarkCacheGetConcurrent(b *testing.B, exp time.Duration) {
  1433. b.StopTimer()
  1434. tc := New(exp, 0)
  1435. tc.Set("foo", "bar", DefaultExpiration)
  1436. wg := new(sync.WaitGroup)
  1437. workers := runtime.NumCPU()
  1438. each := b.N / workers
  1439. wg.Add(workers)
  1440. b.StartTimer()
  1441. for i := 0; i < workers; i++ {
  1442. go func() {
  1443. for j := 0; j < each; j++ {
  1444. tc.Get("foo")
  1445. }
  1446. wg.Done()
  1447. }()
  1448. }
  1449. wg.Wait()
  1450. }
  1451. func BenchmarkCacheWithLRUGetConcurrentExpiring(b *testing.B) {
  1452. benchmarkCacheWithLRUGetConcurrent(b, 5*time.Minute, 10)
  1453. }
  1454. func BenchmarkCacheWithLRUGetConcurrentNotExpiring(b *testing.B) {
  1455. benchmarkCacheWithLRUGetConcurrent(b, NoExpiration, 10)
  1456. }
  1457. func benchmarkCacheWithLRUGetConcurrent(b *testing.B, exp time.Duration, max int) {
  1458. b.StopTimer()
  1459. tc := NewWithLRU(exp, 0, max)
  1460. tc.Set("foo", "bar", DefaultExpiration)
  1461. wg := new(sync.WaitGroup)
  1462. workers := runtime.NumCPU()
  1463. each := b.N / workers
  1464. wg.Add(workers)
  1465. b.StartTimer()
  1466. for i := 0; i < workers; i++ {
  1467. go func() {
  1468. for j := 0; j < each; j++ {
  1469. tc.Get("foo")
  1470. }
  1471. wg.Done()
  1472. }()
  1473. }
  1474. wg.Wait()
  1475. }
  1476. func BenchmarkRWMutexMapGetConcurrent(b *testing.B) {
  1477. b.StopTimer()
  1478. m := map[string]string{
  1479. "foo": "bar",
  1480. }
  1481. mu := sync.RWMutex{}
  1482. wg := new(sync.WaitGroup)
  1483. workers := runtime.NumCPU()
  1484. each := b.N / workers
  1485. wg.Add(workers)
  1486. b.StartTimer()
  1487. for i := 0; i < workers; i++ {
  1488. go func() {
  1489. for j := 0; j < each; j++ {
  1490. mu.RLock()
  1491. _, _ = m["foo"]
  1492. mu.RUnlock()
  1493. }
  1494. wg.Done()
  1495. }()
  1496. }
  1497. wg.Wait()
  1498. }
  1499. func BenchmarkCacheGetManyConcurrentExpiring(b *testing.B) {
  1500. benchmarkCacheGetManyConcurrent(b, 5*time.Minute)
  1501. }
  1502. func BenchmarkCacheGetManyConcurrentNotExpiring(b *testing.B) {
  1503. benchmarkCacheGetManyConcurrent(b, NoExpiration)
  1504. }
  1505. func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
  1506. // This is the same as BenchmarkCacheGetConcurrent, but its result
  1507. // can be compared against BenchmarkShardedCacheGetManyConcurrent
  1508. // in sharded_test.go.
  1509. b.StopTimer()
  1510. n := 10000
  1511. tc := New(exp, 0)
  1512. keys := make([]string, n)
  1513. for i := 0; i < n; i++ {
  1514. k := "foo" + strconv.Itoa(n)
  1515. keys[i] = k
  1516. tc.Set(k, "bar", DefaultExpiration)
  1517. }
  1518. each := b.N / n
  1519. wg := new(sync.WaitGroup)
  1520. wg.Add(n)
  1521. for _, v := range keys {
  1522. go func(key string) {
  1523. for j := 0; j < each; j++ {
  1524. tc.Get(key)
  1525. }
  1526. wg.Done()
  1527. }(v)
  1528. }
  1529. b.StartTimer()
  1530. wg.Wait()
  1531. }
  1532. func BenchmarkCacheWithLRUGetManyConcurrentExpiring(b *testing.B) {
  1533. benchmarkCacheWithLRUGetManyConcurrent(b, 5*time.Minute, 10000)
  1534. }
  1535. func BenchmarkCacheWithLRUGetManyConcurrentNotExpiring(b *testing.B) {
  1536. benchmarkCacheWithLRUGetManyConcurrent(b, NoExpiration, 10000)
  1537. }
  1538. func benchmarkCacheWithLRUGetManyConcurrent(b *testing.B, exp time.Duration, max int) {
  1539. // This is the same as BenchmarkCacheWithLRUGetConcurrent, but its result
  1540. // can be compared against BenchmarkShardedCacheWithLRUGetManyConcurrent
  1541. // in sharded_test.go.
  1542. b.StopTimer()
  1543. n := 10000
  1544. tc := NewWithLRU(exp, 0, max)
  1545. keys := make([]string, n)
  1546. for i := 0; i < n; i++ {
  1547. k := "foo" + strconv.Itoa(n)
  1548. keys[i] = k
  1549. tc.Set(k, "bar", DefaultExpiration)
  1550. }
  1551. each := b.N / n
  1552. wg := new(sync.WaitGroup)
  1553. wg.Add(n)
  1554. for _, v := range keys {
  1555. go func(key string) {
  1556. for j := 0; j < each; j++ {
  1557. tc.Get(key)
  1558. }
  1559. wg.Done()
  1560. }(v)
  1561. }
  1562. b.StartTimer()
  1563. wg.Wait()
  1564. }
  1565. func BenchmarkCacheSetExpiring(b *testing.B) {
  1566. benchmarkCacheSet(b, 5*time.Minute)
  1567. }
  1568. func BenchmarkCacheSetNotExpiring(b *testing.B) {
  1569. benchmarkCacheSet(b, NoExpiration)
  1570. }
  1571. func benchmarkCacheSet(b *testing.B, exp time.Duration) {
  1572. b.StopTimer()
  1573. tc := New(exp, 0)
  1574. b.StartTimer()
  1575. for i := 0; i < b.N; i++ {
  1576. tc.Set("foo", "bar", DefaultExpiration)
  1577. }
  1578. }
  1579. func BenchmarkRWMutexMapSet(b *testing.B) {
  1580. b.StopTimer()
  1581. m := map[string]string{}
  1582. mu := sync.RWMutex{}
  1583. b.StartTimer()
  1584. for i := 0; i < b.N; i++ {
  1585. mu.Lock()
  1586. m["foo"] = "bar"
  1587. mu.Unlock()
  1588. }
  1589. }
  1590. func BenchmarkCacheSetDelete(b *testing.B) {
  1591. b.StopTimer()
  1592. tc := New(DefaultExpiration, 0)
  1593. b.StartTimer()
  1594. for i := 0; i < b.N; i++ {
  1595. tc.Set("foo", "bar", DefaultExpiration)
  1596. tc.Delete("foo")
  1597. }
  1598. }
  1599. func BenchmarkRWMutexMapSetDelete(b *testing.B) {
  1600. b.StopTimer()
  1601. m := map[string]string{}
  1602. mu := sync.RWMutex{}
  1603. b.StartTimer()
  1604. for i := 0; i < b.N; i++ {
  1605. mu.Lock()
  1606. m["foo"] = "bar"
  1607. mu.Unlock()
  1608. mu.Lock()
  1609. delete(m, "foo")
  1610. mu.Unlock()
  1611. }
  1612. }
  1613. func BenchmarkCacheSetDeleteSingleLock(b *testing.B) {
  1614. b.StopTimer()
  1615. tc := New(DefaultExpiration, 0)
  1616. b.StartTimer()
  1617. for i := 0; i < b.N; i++ {
  1618. tc.mu.Lock()
  1619. tc.set("foo", "bar", DefaultExpiration)
  1620. tc.delete("foo")
  1621. tc.mu.Unlock()
  1622. }
  1623. }
  1624. func BenchmarkRWMutexMapSetDeleteSingleLock(b *testing.B) {
  1625. b.StopTimer()
  1626. m := map[string]string{}
  1627. mu := sync.RWMutex{}
  1628. b.StartTimer()
  1629. for i := 0; i < b.N; i++ {
  1630. mu.Lock()
  1631. m["foo"] = "bar"
  1632. delete(m, "foo")
  1633. mu.Unlock()
  1634. }
  1635. }
  1636. func BenchmarkIncrementInt(b *testing.B) {
  1637. b.StopTimer()
  1638. tc := New(DefaultExpiration, 0)
  1639. tc.Set("foo", 0, DefaultExpiration)
  1640. b.StartTimer()
  1641. for i := 0; i < b.N; i++ {
  1642. tc.IncrementInt("foo", 1)
  1643. }
  1644. }
  1645. func BenchmarkDeleteExpiredLoop(b *testing.B) {
  1646. b.StopTimer()
  1647. tc := New(5*time.Minute, 0)
  1648. tc.mu.Lock()
  1649. for i := 0; i < 100000; i++ {
  1650. tc.set(strconv.Itoa(i), "bar", DefaultExpiration)
  1651. }
  1652. tc.mu.Unlock()
  1653. b.StartTimer()
  1654. for i := 0; i < b.N; i++ {
  1655. tc.DeleteExpired()
  1656. }
  1657. }
  1658. func TestGetWithExpiration(t *testing.T) {
  1659. tc := New(DefaultExpiration, 0)
  1660. a, expiration, found := tc.GetWithExpiration("a")
  1661. if found || a != nil || !expiration.IsZero() {
  1662. t.Error("Getting A found value that shouldn't exist:", a)
  1663. }
  1664. b, expiration, found := tc.GetWithExpiration("b")
  1665. if found || b != nil || !expiration.IsZero() {
  1666. t.Error("Getting B found value that shouldn't exist:", b)
  1667. }
  1668. c, expiration, found := tc.GetWithExpiration("c")
  1669. if found || c != nil || !expiration.IsZero() {
  1670. t.Error("Getting C found value that shouldn't exist:", c)
  1671. }
  1672. tc.Set("a", 1, DefaultExpiration)
  1673. tc.Set("b", "b", DefaultExpiration)
  1674. tc.Set("c", 3.5, DefaultExpiration)
  1675. tc.Set("d", 1, NoExpiration)
  1676. tc.Set("e", 1, 50*time.Millisecond)
  1677. x, expiration, found := tc.GetWithExpiration("a")
  1678. if !found {
  1679. t.Error("a was not found while getting a2")
  1680. }
  1681. if x == nil {
  1682. t.Error("x for a is nil")
  1683. } else if a2 := x.(int); a2+2 != 3 {
  1684. t.Error("a2 (which should be 1) plus 2 does not equal 3; value:", a2)
  1685. }
  1686. if !expiration.IsZero() {
  1687. t.Error("expiration for a is not a zeroed time")
  1688. }
  1689. x, expiration, found = tc.GetWithExpiration("b")
  1690. if !found {
  1691. t.Error("b was not found while getting b2")
  1692. }
  1693. if x == nil {
  1694. t.Error("x for b is nil")
  1695. } else if b2 := x.(string); b2+"B" != "bB" {
  1696. t.Error("b2 (which should be b) plus B does not equal bB; value:", b2)
  1697. }
  1698. if !expiration.IsZero() {
  1699. t.Error("expiration for b is not a zeroed time")
  1700. }
  1701. x, expiration, found = tc.GetWithExpiration("c")
  1702. if !found {
  1703. t.Error("c was not found while getting c2")
  1704. }
  1705. if x == nil {
  1706. t.Error("x for c is nil")
  1707. } else if c2 := x.(float64); c2+1.2 != 4.7 {
  1708. t.Error("c2 (which should be 3.5) plus 1.2 does not equal 4.7; value:", c2)
  1709. }
  1710. if !expiration.IsZero() {
  1711. t.Error("expiration for c is not a zeroed time")
  1712. }
  1713. x, expiration, found = tc.GetWithExpiration("d")
  1714. if !found {
  1715. t.Error("d was not found while getting d2")
  1716. }
  1717. if x == nil {
  1718. t.Error("x for d is nil")
  1719. } else if d2 := x.(int); d2+2 != 3 {
  1720. t.Error("d (which should be 1) plus 2 does not equal 3; value:", d2)
  1721. }
  1722. if !expiration.IsZero() {
  1723. t.Error("expiration for d is not a zeroed time")
  1724. }
  1725. x, expiration, found = tc.GetWithExpiration("e")
  1726. if !found {
  1727. t.Error("e was not found while getting e2")
  1728. }
  1729. if x == nil {
  1730. t.Error("x for e is nil")
  1731. } else if e2 := x.(int); e2+2 != 3 {
  1732. t.Error("e (which should be 1) plus 2 does not equal 3; value:", e2)
  1733. }
  1734. if expiration.UnixNano() != tc.items["e"].Expiration {
  1735. t.Error("expiration for e is not the correct time")
  1736. }
  1737. if expiration.UnixNano() < time.Now().UnixNano() {
  1738. t.Error("expiration for e is in the past")
  1739. }
  1740. }