envValues_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. package env
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sync"
  6. "testing"
  7. )
  8. func TestSetError(t *testing.T) {
  9. envParent := NewEnv()
  10. envChild := envParent.NewEnv()
  11. err := envChild.Set("a", "a")
  12. if err == nil {
  13. t.Errorf("Set error - received: %v - expected: %v", err, fmt.Errorf("undefined symbol 'a'"))
  14. } else if err.Error() != "undefined symbol 'a'" {
  15. t.Errorf("Set error - received: %v - expected: %v", err, fmt.Errorf("undefined symbol 'a'"))
  16. }
  17. }
  18. func TestAddrError(t *testing.T) {
  19. envParent := NewEnv()
  20. envChild := envParent.NewEnv()
  21. _, err := envChild.Addr("a")
  22. if err == nil {
  23. t.Errorf("Addr error - received: %v - expected: %v", err, fmt.Errorf("undefined symbol 'a'"))
  24. } else if err.Error() != "undefined symbol 'a'" {
  25. t.Errorf("Addr error - received: %v - expected: %v", err, fmt.Errorf("undefined symbol 'a'"))
  26. }
  27. }
  28. func TestDefineGlobalValue(t *testing.T) {
  29. envParent := NewEnv()
  30. envChild := envParent.NewEnv()
  31. err := envChild.DefineGlobalValue("a", reflect.ValueOf("a"))
  32. if err != nil {
  33. t.Fatal("DefineGlobalValue error:", err)
  34. }
  35. var value interface{}
  36. value, err = envParent.Get("a")
  37. if err != nil {
  38. t.Fatal("Get error:", err)
  39. }
  40. v, ok := value.(string)
  41. if !ok {
  42. t.Fatalf("value - received: %T - expected: %T", value, "a")
  43. }
  44. if v != "a" {
  45. t.Fatalf("value - received: %v - expected: %v", v, "a")
  46. }
  47. }
  48. func TestDefineAndGet(t *testing.T) {
  49. var err error
  50. var value interface{}
  51. tests := []struct {
  52. testInfo string
  53. varName string
  54. varDefineValue interface{}
  55. varGetValue interface{}
  56. varKind reflect.Kind
  57. defineError error
  58. getError error
  59. }{
  60. {testInfo: "nil", varName: "a", varDefineValue: reflect.Value{}, varGetValue: reflect.Value{}, varKind: reflect.Invalid},
  61. {testInfo: "nil", varName: "a", varDefineValue: nil, varGetValue: nil, varKind: reflect.Interface},
  62. {testInfo: "bool", varName: "a", varDefineValue: true, varGetValue: true, varKind: reflect.Bool},
  63. {testInfo: "int16", varName: "a", varDefineValue: int16(1), varGetValue: int16(1), varKind: reflect.Int16},
  64. {testInfo: "int32", varName: "a", varDefineValue: int32(1), varGetValue: int32(1), varKind: reflect.Int32},
  65. {testInfo: "int64", varName: "a", varDefineValue: int64(1), varGetValue: int64(1), varKind: reflect.Int64},
  66. {testInfo: "uint32", varName: "a", varDefineValue: uint32(1), varGetValue: uint32(1), varKind: reflect.Uint32},
  67. {testInfo: "uint64", varName: "a", varDefineValue: uint64(1), varGetValue: uint64(1), varKind: reflect.Uint64},
  68. {testInfo: "float32", varName: "a", varDefineValue: float32(1), varGetValue: float32(1), varKind: reflect.Float32},
  69. {testInfo: "float64", varName: "a", varDefineValue: float64(1), varGetValue: float64(1), varKind: reflect.Float64},
  70. {testInfo: "string", varName: "a", varDefineValue: "a", varGetValue: "a", varKind: reflect.String},
  71. {testInfo: "string with dot", varName: "a.a", varDefineValue: "a", varGetValue: nil, varKind: reflect.Interface, defineError: ErrSymbolContainsDot, getError: fmt.Errorf("undefined symbol 'a.a'")},
  72. {testInfo: "string with quotes", varName: "a", varDefineValue: `"a"`, varGetValue: `"a"`, varKind: reflect.String},
  73. }
  74. // DefineAndGet
  75. for _, test := range tests {
  76. env := NewEnv()
  77. err = env.Define(test.varName, test.varDefineValue)
  78. if err != nil && test.defineError != nil {
  79. if err.Error() != test.defineError.Error() {
  80. t.Errorf("DefineAndGet %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  81. continue
  82. }
  83. } else if err != test.defineError {
  84. t.Errorf("DefineAndGet %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  85. continue
  86. }
  87. value, err = env.Get(test.varName)
  88. if err != nil && test.getError != nil {
  89. if err.Error() != test.getError.Error() {
  90. t.Errorf("DefineAndGet %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  91. continue
  92. }
  93. } else if err != test.getError {
  94. t.Errorf("DefineAndGet %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  95. continue
  96. }
  97. if value != test.varGetValue {
  98. t.Errorf("DefineAndGet %v - value check - received %#v expected: %#v", test.testInfo, value, test.varGetValue)
  99. }
  100. }
  101. // DefineAndGet NewEnv
  102. for _, test := range tests {
  103. envParent := NewEnv()
  104. envChild := envParent.NewEnv()
  105. err = envParent.Define(test.varName, test.varDefineValue)
  106. if err != nil && test.defineError != nil {
  107. if err.Error() != test.defineError.Error() {
  108. t.Errorf("DefineAndGet NewEnv %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  109. continue
  110. }
  111. } else if err != test.defineError {
  112. t.Errorf("DefineAndGet NewEnv %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  113. continue
  114. }
  115. value, err = envChild.Get(test.varName)
  116. if err != nil && test.getError != nil {
  117. if err.Error() != test.getError.Error() {
  118. t.Errorf("DefineAndGet NewEnv %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  119. continue
  120. }
  121. } else if err != test.getError {
  122. t.Errorf("DefineAndGet NewEnv %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  123. continue
  124. }
  125. if value != test.varGetValue {
  126. t.Errorf("DefineAndGet NewEnv %v - value check - received %#v expected: %#v", test.testInfo, value, test.varGetValue)
  127. }
  128. }
  129. // DefineAndGet DefineGlobal
  130. for _, test := range tests {
  131. envParent := NewEnv()
  132. envChild := envParent.NewEnv()
  133. err = envChild.DefineGlobal(test.varName, test.varDefineValue)
  134. if err != nil && test.defineError != nil {
  135. if err.Error() != test.defineError.Error() {
  136. t.Errorf("DefineAndGet DefineGlobal %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  137. continue
  138. }
  139. } else if err != test.defineError {
  140. t.Errorf("DefineAndGet DefineGlobal %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  141. continue
  142. }
  143. value, err = envParent.Get(test.varName)
  144. if err != nil && test.getError != nil {
  145. if err.Error() != test.getError.Error() {
  146. t.Errorf("DefineAndGet DefineGlobal %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  147. continue
  148. }
  149. } else if err != test.getError {
  150. t.Errorf("DefineAndGet DefineGlobal %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  151. continue
  152. }
  153. if value != test.varGetValue {
  154. t.Errorf("DefineAndGet DefineGlobal %v - value check - received %#v expected: %#v", test.testInfo, value, test.varGetValue)
  155. }
  156. }
  157. }
  158. func TestDefineModify(t *testing.T) {
  159. var err error
  160. var value interface{}
  161. tests := []struct {
  162. testInfo string
  163. varName string
  164. varDefineValue interface{}
  165. varGetValue interface{}
  166. varKind reflect.Kind
  167. defineError error
  168. getError error
  169. }{
  170. {testInfo: "nil", varName: "a", varDefineValue: nil, varGetValue: nil, varKind: reflect.Interface},
  171. {testInfo: "bool", varName: "a", varDefineValue: true, varGetValue: true, varKind: reflect.Bool},
  172. {testInfo: "int64", varName: "a", varDefineValue: int64(1), varGetValue: int64(1), varKind: reflect.Int64},
  173. {testInfo: "float64", varName: "a", varDefineValue: float64(1), varGetValue: float64(1), varKind: reflect.Float64},
  174. {testInfo: "string", varName: "a", varDefineValue: "a", varGetValue: "a", varKind: reflect.String},
  175. }
  176. changeTests := []struct {
  177. varDefineValue interface{}
  178. varGetValue interface{}
  179. varKind reflect.Kind
  180. defineError error
  181. getError error
  182. }{
  183. {varDefineValue: nil, varGetValue: nil, varKind: reflect.Interface},
  184. {varDefineValue: "a", varGetValue: "a", varKind: reflect.String},
  185. {varDefineValue: int64(1), varGetValue: int64(1), varKind: reflect.Int64},
  186. {varDefineValue: float64(1), varGetValue: float64(1), varKind: reflect.Float64},
  187. {varDefineValue: true, varGetValue: true, varKind: reflect.Bool},
  188. }
  189. // DefineModify
  190. for _, test := range tests {
  191. env := NewEnv()
  192. err = env.Define(test.varName, test.varDefineValue)
  193. if err != nil && test.defineError != nil {
  194. if err.Error() != test.defineError.Error() {
  195. t.Errorf("DefineModify %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  196. continue
  197. }
  198. } else if err != test.defineError {
  199. t.Errorf("DefineModify %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  200. continue
  201. }
  202. value, err = env.Get(test.varName)
  203. if err != nil && test.getError != nil {
  204. if err.Error() != test.getError.Error() {
  205. t.Errorf("DefineModify %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  206. continue
  207. }
  208. } else if err != test.getError {
  209. t.Errorf("DefineModify %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  210. continue
  211. }
  212. if value != test.varGetValue {
  213. t.Errorf("DefineModify %v - value check - received %#v expected: %#v", test.testInfo, value, test.varGetValue)
  214. }
  215. // DefineModify changeTest
  216. for _, changeTest := range changeTests {
  217. err = env.Set(test.varName, changeTest.varDefineValue)
  218. if err != nil && changeTest.defineError != nil {
  219. if err.Error() != changeTest.defineError.Error() {
  220. t.Errorf("DefineModify changeTest %v - Set error - received: %v - expected: %v", test.testInfo, err, changeTest.defineError)
  221. continue
  222. }
  223. } else if err != changeTest.defineError {
  224. t.Errorf("DefineModify changeTest %v - Set error - received: %v - expected: %v", test.testInfo, err, changeTest.defineError)
  225. continue
  226. }
  227. value, err = env.Get(test.varName)
  228. if err != nil && changeTest.getError != nil {
  229. if err.Error() != changeTest.getError.Error() {
  230. t.Errorf("DefineModify changeTest %v - Get error - received: %v - expected: %v", test.testInfo, err, changeTest.getError)
  231. continue
  232. }
  233. } else if err != changeTest.getError {
  234. t.Errorf("DefineModify changeTest %v - Get error - received: %v - expected: %v", test.testInfo, err, changeTest.getError)
  235. continue
  236. }
  237. if value != changeTest.varGetValue {
  238. t.Errorf("DefineModify changeTest %v - value check - received %#v expected: %#v", test.testInfo, value, changeTest.varGetValue)
  239. }
  240. }
  241. }
  242. // DefineModify envParent
  243. for _, test := range tests {
  244. envParent := NewEnv()
  245. envChild := envParent.NewEnv()
  246. err = envParent.Define(test.varName, test.varDefineValue)
  247. if err != nil && test.defineError != nil {
  248. if err.Error() != test.defineError.Error() {
  249. t.Errorf("DefineModify envParent %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  250. continue
  251. }
  252. } else if err != test.defineError {
  253. t.Errorf("DefineModify envParent %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  254. continue
  255. }
  256. value, err = envChild.Get(test.varName)
  257. if err != nil && test.getError != nil {
  258. if err.Error() != test.getError.Error() {
  259. t.Errorf("DefineModify envParent %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  260. continue
  261. }
  262. } else if err != test.getError {
  263. t.Errorf("DefineModify envParent %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  264. continue
  265. }
  266. if value != test.varGetValue {
  267. t.Errorf("DefineModify envParent %v - value check - received %#v expected: %#v", test.testInfo, value, test.varGetValue)
  268. }
  269. for _, changeTest := range changeTests {
  270. err = envParent.Set(test.varName, changeTest.varDefineValue)
  271. if err != nil && changeTest.defineError != nil {
  272. if err.Error() != changeTest.defineError.Error() {
  273. t.Errorf("DefineModify envParent changeTest %v - Set error - received: %v - expected: %v", test.testInfo, err, changeTest.defineError)
  274. continue
  275. }
  276. } else if err != changeTest.defineError {
  277. t.Errorf("DefineModify envParent changeTest %v - Set error - received: %v - expected: %v", test.testInfo, err, changeTest.defineError)
  278. continue
  279. }
  280. value, err = envChild.Get(test.varName)
  281. if err != nil && changeTest.getError != nil {
  282. if err.Error() != changeTest.getError.Error() {
  283. t.Errorf("DefineModify envParent changeTest %v - Get error - received: %v - expected: %v", test.testInfo, err, changeTest.getError)
  284. continue
  285. }
  286. } else if err != changeTest.getError {
  287. t.Errorf("ChanDefineModify envParent changeTestgeTest %v - Get error - received: %v - expected: %v", test.testInfo, err, changeTest.getError)
  288. continue
  289. }
  290. if value != changeTest.varGetValue {
  291. t.Errorf("DefineModify envParent changeTest %v - value check - received %#v expected: %#v", test.testInfo, value, changeTest.varGetValue)
  292. }
  293. }
  294. }
  295. // DefineModify envChild
  296. for _, test := range tests {
  297. envParent := NewEnv()
  298. envChild := envParent.NewEnv()
  299. err = envParent.Define(test.varName, test.varDefineValue)
  300. if err != nil && test.defineError != nil {
  301. if err.Error() != test.defineError.Error() {
  302. t.Errorf("DefineModify envChild %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  303. continue
  304. }
  305. } else if err != test.defineError {
  306. t.Errorf("DefineModify envChild %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  307. continue
  308. }
  309. value, err = envChild.Get(test.varName)
  310. if err != nil && test.getError != nil {
  311. if err.Error() != test.getError.Error() {
  312. t.Errorf("DefineModify envChild %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  313. continue
  314. }
  315. } else if err != test.getError {
  316. t.Errorf("DefineModify envChild %v - Get error - received: %v - expected: %v", test.testInfo, err, test.getError)
  317. continue
  318. }
  319. if value != test.varGetValue {
  320. t.Errorf("DefineModify envChild %v - value check - received %#v expected: %#v", test.testInfo, value, test.varGetValue)
  321. }
  322. for _, changeTest := range changeTests {
  323. err = envChild.Set(test.varName, changeTest.varDefineValue)
  324. if err != nil && changeTest.defineError != nil {
  325. if err.Error() != changeTest.defineError.Error() {
  326. t.Errorf("DefineModify envChild changeTest %v - Set error - received: %v - expected: %v", test.testInfo, err, changeTest.defineError)
  327. continue
  328. }
  329. } else if err != changeTest.defineError {
  330. t.Errorf("DefineModify envChild changeTest %v - Set error - received: %v - expected: %v", test.testInfo, err, changeTest.defineError)
  331. continue
  332. }
  333. value, err = envChild.Get(test.varName)
  334. if err != nil && changeTest.getError != nil {
  335. if err.Error() != changeTest.getError.Error() {
  336. t.Errorf("DefineModify envChild changeTest %v - Get error - received: %v - expected: %v", test.testInfo, err, changeTest.getError)
  337. continue
  338. }
  339. } else if err != changeTest.getError {
  340. t.Errorf("ChanDefineModify envChild changeTestgeTest %v - Get error - received: %v - expected: %v", test.testInfo, err, changeTest.getError)
  341. continue
  342. }
  343. if value != changeTest.varGetValue {
  344. t.Errorf("DefineModify envChild changeTest %v - value check - received %#v expected: %#v", test.testInfo, value, changeTest.varGetValue)
  345. }
  346. }
  347. }
  348. }
  349. func TestAddr(t *testing.T) {
  350. var err error
  351. tests := []struct {
  352. testInfo string
  353. varName string
  354. varDefineValue interface{}
  355. defineError error
  356. addrError error
  357. }{
  358. {testInfo: "nil", varName: "a", varDefineValue: nil, addrError: nil},
  359. {testInfo: "string", varName: "a", varDefineValue: "a", addrError: fmt.Errorf("unaddressable")},
  360. {testInfo: "int64", varName: "a", varDefineValue: int64(1), addrError: fmt.Errorf("unaddressable")},
  361. {testInfo: "float64", varName: "a", varDefineValue: float64(1), addrError: fmt.Errorf("unaddressable")},
  362. {testInfo: "bool", varName: "a", varDefineValue: true, addrError: fmt.Errorf("unaddressable")},
  363. }
  364. // TestAddr
  365. for _, test := range tests {
  366. envParent := NewEnv()
  367. envChild := envParent.NewEnv()
  368. err = envParent.Define(test.varName, test.varDefineValue)
  369. if err != nil && test.defineError != nil {
  370. if err.Error() != test.defineError.Error() {
  371. t.Errorf("TestAddr %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  372. continue
  373. }
  374. } else if err != test.defineError {
  375. t.Errorf("TestAddr %v - Define error - received: %v - expected: %v", test.testInfo, err, test.defineError)
  376. continue
  377. }
  378. _, err = envChild.Addr(test.varName)
  379. if err != nil && test.addrError != nil {
  380. if err.Error() != test.addrError.Error() {
  381. t.Errorf("TestAddr %v - Addr error - received: %v - expected: %v", test.testInfo, err, test.addrError)
  382. continue
  383. }
  384. } else if err != test.addrError {
  385. t.Errorf("TestAddr %v - Addr error - received: %v - expected: %v", test.testInfo, err, test.addrError)
  386. continue
  387. }
  388. }
  389. }
  390. func TestDelete(t *testing.T) {
  391. // empty
  392. env := NewEnv()
  393. env.Delete("a")
  394. // add & delete a
  395. env.Define("a", "a")
  396. env.Delete("a")
  397. value, err := env.Get("a")
  398. expectedError := "undefined symbol 'a'"
  399. if err == nil || err.Error() != expectedError {
  400. t.Errorf("Get error - received: %v - expected: %v", err, expectedError)
  401. }
  402. if value != nil {
  403. t.Errorf("Get value - received: %#v - expected: %#v", value, nil)
  404. }
  405. }
  406. func TestDeleteGlobal(t *testing.T) {
  407. // empty
  408. env := NewEnv()
  409. env.DeleteGlobal("a")
  410. // add & delete a
  411. env.Define("a", "a")
  412. env.DeleteGlobal("a")
  413. value, err := env.Get("a")
  414. expectedError := "undefined symbol 'a'"
  415. if err == nil || err.Error() != expectedError {
  416. t.Errorf("Get error - received: %v - expected: %v", err, expectedError)
  417. }
  418. if value != nil {
  419. t.Errorf("Get value - received: %#v - expected: %#v", value, nil)
  420. }
  421. // parent & child, var in child, delete in parent
  422. envChild := env.NewEnv()
  423. envChild.Define("a", "a")
  424. env.DeleteGlobal("a")
  425. value, err = envChild.Get("a")
  426. if err != nil {
  427. t.Errorf("Get error - received: %v - expected: %v", err, nil)
  428. }
  429. if value != "a" {
  430. t.Errorf("Get value - received: %#v - expected: %#v", value, "a")
  431. }
  432. // parent & child, var in child, delete in child
  433. envChild.DeleteGlobal("a")
  434. value, err = envChild.Get("a")
  435. if err == nil || err.Error() != expectedError {
  436. t.Errorf("Get error - received: %v - expected: %v", err, expectedError)
  437. }
  438. if value != nil {
  439. t.Errorf("Get value - received: %#v - expected: %#v", value, nil)
  440. }
  441. // parent & child, var in parent, delete in child
  442. env.Define("a", "a")
  443. envChild.DeleteGlobal("a")
  444. value, err = envChild.Get("a")
  445. if err == nil || err.Error() != expectedError {
  446. t.Errorf("Get error - received: %v - expected: %v", err, expectedError)
  447. }
  448. if value != nil {
  449. t.Errorf("Get value - received: %#v - expected: %#v", value, nil)
  450. }
  451. // parent & child, var in parent, delete in parent
  452. env.Define("a", "a")
  453. env.DeleteGlobal("a")
  454. value, err = envChild.Get("a")
  455. if err == nil || err.Error() != expectedError {
  456. t.Errorf("Get error - received: %v - expected: %v", err, expectedError)
  457. }
  458. if value != nil {
  459. t.Errorf("Get value - received: %#v - expected: %#v", value, nil)
  460. }
  461. }
  462. func TestRaceCreateSameVariable(t *testing.T) {
  463. // Test creating same variable in parallel
  464. waitChan := make(chan struct{}, 1)
  465. var waitGroup sync.WaitGroup
  466. env := NewEnv()
  467. for i := 0; i < 100; i++ {
  468. waitGroup.Add(1)
  469. go func(i int) {
  470. <-waitChan
  471. err := env.Define("a", i)
  472. if err != nil {
  473. t.Errorf("Define error: %v", err)
  474. }
  475. _, err = env.Get("a")
  476. if err != nil {
  477. t.Errorf("Get error: %v", err)
  478. }
  479. waitGroup.Done()
  480. }(i)
  481. }
  482. close(waitChan)
  483. waitGroup.Wait()
  484. _, err := env.Get("a")
  485. if err != nil {
  486. t.Errorf("Get error: %v", err)
  487. }
  488. }
  489. func TestRaceCreateDifferentVariables(t *testing.T) {
  490. // Test creating different variables in parallel
  491. waitChan := make(chan struct{}, 1)
  492. var waitGroup sync.WaitGroup
  493. env := NewEnv()
  494. for i := 0; i < 100; i++ {
  495. waitGroup.Add(1)
  496. go func(i int) {
  497. <-waitChan
  498. err := env.Define(fmt.Sprint(i), i)
  499. if err != nil {
  500. t.Errorf("Define error: %v", err)
  501. }
  502. _, err = env.Get(fmt.Sprint(i))
  503. if err != nil {
  504. t.Errorf("Get error: %v", err)
  505. }
  506. waitGroup.Done()
  507. }(i)
  508. }
  509. close(waitChan)
  510. waitGroup.Wait()
  511. for i := 0; i < 100; i++ {
  512. _, err := env.Get(fmt.Sprint(i))
  513. if err != nil {
  514. t.Errorf("Get error: %v", err)
  515. }
  516. }
  517. }
  518. func TestRaceReadDifferentVariables(t *testing.T) {
  519. // Test reading different variables in parallel
  520. waitChan := make(chan struct{}, 1)
  521. var waitGroup sync.WaitGroup
  522. env := NewEnv()
  523. for i := 0; i < 100; i++ {
  524. err := env.Define(fmt.Sprint(i), i)
  525. if err != nil {
  526. t.Errorf("Define error: %v", err)
  527. }
  528. _, err = env.Get(fmt.Sprint(i))
  529. if err != nil {
  530. t.Errorf("Get error: %v", err)
  531. }
  532. }
  533. for i := 0; i < 100; i++ {
  534. waitGroup.Add(1)
  535. go func(i int) {
  536. <-waitChan
  537. _, err := env.Get(fmt.Sprint(i))
  538. if err != nil {
  539. t.Errorf("Get error: %v", err)
  540. }
  541. waitGroup.Done()
  542. }(i)
  543. }
  544. close(waitChan)
  545. waitGroup.Wait()
  546. }
  547. func TestRaceSetSameVariable(t *testing.T) {
  548. // Test setting same variable in parallel
  549. waitChan := make(chan struct{}, 1)
  550. var waitGroup sync.WaitGroup
  551. env := NewEnv()
  552. err := env.Define("a", 0)
  553. if err != nil {
  554. t.Errorf("Define error: %v", err)
  555. }
  556. _, err = env.Get("a")
  557. if err != nil {
  558. t.Errorf("Get error: %v", err)
  559. }
  560. for i := 0; i < 100; i++ {
  561. waitGroup.Add(1)
  562. go func(i int) {
  563. <-waitChan
  564. err := env.Set("a", i)
  565. if err != nil {
  566. t.Errorf("Set error: %v", err)
  567. }
  568. waitGroup.Done()
  569. }(i)
  570. }
  571. close(waitChan)
  572. waitGroup.Wait()
  573. _, err = env.Get("a")
  574. if err != nil {
  575. t.Errorf("Get error: %v", err)
  576. }
  577. }
  578. func TestRaceSetSameVariableNewEnv(t *testing.T) {
  579. // Test setting same variable in parallel with NewEnv
  580. waitChan := make(chan struct{}, 1)
  581. var waitGroup sync.WaitGroup
  582. env := NewEnv()
  583. err := env.Define("a", 0)
  584. if err != nil {
  585. t.Errorf("Define error: %v", err)
  586. }
  587. _, err = env.Get("a")
  588. if err != nil {
  589. t.Errorf("Get error: %v", err)
  590. }
  591. for i := 0; i < 100; i++ {
  592. waitGroup.Add(1)
  593. go func(i int) {
  594. <-waitChan
  595. env = env.NewEnv().NewEnv()
  596. err := env.Set("a", i)
  597. if err != nil {
  598. t.Errorf("Set error: %v", err)
  599. }
  600. waitGroup.Done()
  601. }(i)
  602. }
  603. }
  604. func TestRaceDefineAndSetSameVariable(t *testing.T) {
  605. // Test defining and setting same variable in parallel
  606. for i := 0; i < 100; i++ {
  607. raceDefineAndSetSameVariable(t)
  608. }
  609. }
  610. func raceDefineAndSetSameVariable(t *testing.T) {
  611. waitChan := make(chan struct{}, 1)
  612. var waitGroup sync.WaitGroup
  613. envParent := NewEnv()
  614. envChild := envParent.NewEnv()
  615. for i := 0; i < 2; i++ {
  616. waitGroup.Add(1)
  617. go func() {
  618. <-waitChan
  619. err := envParent.Set("a", 1)
  620. if err != nil && err.Error() != "undefined symbol 'a'" {
  621. t.Errorf("Set error: %v", err)
  622. }
  623. waitGroup.Done()
  624. }()
  625. waitGroup.Add(1)
  626. go func() {
  627. <-waitChan
  628. err := envParent.Define("a", 2)
  629. if err != nil {
  630. t.Errorf("Define error: %v", err)
  631. }
  632. waitGroup.Done()
  633. }()
  634. waitGroup.Add(1)
  635. go func() {
  636. <-waitChan
  637. err := envChild.Set("a", 3)
  638. if err != nil && err.Error() != "undefined symbol 'a'" {
  639. t.Errorf("Set error: %v", err)
  640. }
  641. waitGroup.Done()
  642. }()
  643. waitGroup.Add(1)
  644. go func() {
  645. <-waitChan
  646. err := envChild.Define("a", 4)
  647. if err != nil {
  648. t.Errorf("Define error: %v", err)
  649. }
  650. waitGroup.Done()
  651. }()
  652. }
  653. close(waitChan)
  654. waitGroup.Wait()
  655. _, err := envParent.Get("a") // value of a could be 1, 2, or 3
  656. if err != nil {
  657. t.Errorf("Get error: %v", err)
  658. }
  659. _, err = envChild.Get("a") // value of a could be 3 or 4
  660. if err != nil {
  661. t.Errorf("Get error: %v", err)
  662. }
  663. }
  664. func BenchmarkDefine(b *testing.B) {
  665. var err error
  666. env := NewEnv()
  667. b.ResetTimer()
  668. for i := 0; i < b.N; i++ {
  669. err := env.Define("a", 1)
  670. if err != nil {
  671. b.Errorf("Set error: %v", err)
  672. }
  673. }
  674. b.StopTimer()
  675. _, err = env.Get("a")
  676. if err != nil {
  677. b.Errorf("Get error: %v", err)
  678. }
  679. }
  680. func BenchmarkSet(b *testing.B) {
  681. env := NewEnv()
  682. err := env.Define("a", 1)
  683. if err != nil {
  684. b.Errorf("Define error: %v", err)
  685. }
  686. b.ResetTimer()
  687. for i := 0; i < b.N; i++ {
  688. err = env.Set("a", 1)
  689. if err != nil {
  690. b.Errorf("Set error: %v", err)
  691. }
  692. }
  693. b.StopTimer()
  694. _, err = env.Get("a")
  695. if err != nil {
  696. b.Errorf("Get error: %v", err)
  697. }
  698. }