(t *testing.T)
| 447 | } |
| 448 | |
| 449 | func TestConcurrent(t *testing.T) { |
| 450 | slm := NewShardLockMaps() |
| 451 | ch := make(chan int) |
| 452 | const iterations = 1000 |
| 453 | var a [iterations]int |
| 454 | |
| 455 | // Using go routines insert 1000 ints into our map. |
| 456 | go func() { |
| 457 | for i := 0; i < iterations/2; i++ { |
| 458 | // Add item to map. |
| 459 | slm.Set(strconv.Itoa(i), i) |
| 460 | |
| 461 | // Retrieve item from map. |
| 462 | val, _ := slm.Get(strconv.Itoa(i)) |
| 463 | |
| 464 | // Write to channel inserted value. |
| 465 | ch <- val.(int) |
| 466 | } // Call go routine with current index. |
| 467 | }() |
| 468 | |
| 469 | go func() { |
| 470 | for i := iterations / 2; i < iterations; i++ { |
| 471 | // Add item to map. |
| 472 | slm.Set(strconv.Itoa(i), i) |
| 473 | |
| 474 | // Retrieve item from map. |
| 475 | val, _ := slm.Get(strconv.Itoa(i)) |
| 476 | |
| 477 | // Write to channel inserted value. |
| 478 | ch <- val.(int) |
| 479 | } // Call go routine with current index. |
| 480 | }() |
| 481 | |
| 482 | // Wait for all go routines to finish. |
| 483 | counter := 0 |
| 484 | for elem := range ch { |
| 485 | a[counter] = elem |
| 486 | counter++ |
| 487 | if counter == iterations { |
| 488 | break |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // Sorts array, will make is simpler to verify all inserted values we're returned. |
| 493 | sort.Ints(a[0:iterations]) |
| 494 | |
| 495 | // Make sure map contains 1000 elements. |
| 496 | if slm.Count() != iterations { |
| 497 | t.Error("Expecting 1000 elements.") |
| 498 | } |
| 499 | |
| 500 | // Make sure all inserted values we're fetched from map. |
| 501 | for i := 0; i < iterations; i++ { |
| 502 | if i != a[i] { |
| 503 | t.Error("missing value", i) |
| 504 | } |
| 505 | } |
| 506 | } |
nothing calls this directly
no test coverage detected