예제 #1
0
func checkCorrectness(
	t *testing.T, data map[uint32]map[uint32]uint32, h *cart.Handler) {

	for customer, _ := range data {
		r := listRequest(t, "customer", customer)
		w := httptest.NewRecorder()
		h.List(w, r)
		lines := strings.Split(w.Body.String(), "\n")
		for i := 1; i < len(lines); i++ {
			p := strings.Split(lines[i], " ")
			if len(p) < 2 {
				continue
			}
			i, err := strconv.Atoi(p[0])
			if err != nil {
				t.Fatalf("unexpected error: %s", err)
			}

			n, err := strconv.Atoi(p[1])
			if err != nil {
				t.Fatalf("unexpected error: %s", err)
			}

			if data[customer][uint32(i)] != uint32(n) {
				t.Fatalf("customer %v has %v of %v instead of %v",
					customer, n)
			}
		}
	}
}
예제 #2
0
func parallelAdd(t *testing.T, h *cart.Handler,
	wg *sync.WaitGroup,
	data chan map[uint32]map[uint32]uint32) {

	basket := make(map[uint32]map[uint32]uint32)
	w := httptest.NewRecorder()
	var slice []tuple

	for i := 0; i <= NumberOfThreadIterations; i++ {
		var item uint32 = uint32(rand.Intn(200))
		var customer uint32 = uint32(rand.Intn(100))

		i := tuple{customer, item}
		slice = append(slice, i)
	}

	for _, pair := range slice {
		r := modRequest(t, "add", pair.customer, pair.item)
	retry:
		h.Mod(cart.AddToSet)(w, r)

		if w.Code == 503 {
			goto retry
		}

		if basket[pair.customer] == nil {
			basket[pair.customer] = make(map[uint32]uint32)
		}

		basket[pair.customer][pair.item]++
		if !strings.HasPrefix(w.Body.String(), "OK") {
			t.Fatalf("expected `OK`, got `%s`", w.Body.String())
		}
	}

	data <- basket
	wg.Done()
}