func Test_bartender_should_handle_order_and_check_validity_into_redis_with_success(t *testing.T) {
	// given
	order := commons.Order{Id: 1, Quantity: 5, Type: commons.Beer, CallBackUrl: "http://some-callback.com"}
	bd, _ := json.Marshal(order)
	// prepare datum into redis
	c, _ := redis.Dial("tcp", redisAddr)
	defer c.Close()
	c.Do("SET", order.Id, string(bd))
	defer c.Do("DEL", order.Id)
	// start bartender
	startBartender(b)
	// when
	resp, err := http.Post("http://127.0.0.1:4343/orders", "application/json", bytes.NewBuffer(bd))
	assert.Nil(t, err)
	assert.NotNil(t, resp)
	assert.Equal(t, 200, resp.StatusCode)
	val, _ := c.Do("GET", order.Id)
	var upgOrder commons.Order
	unmErr := commons.UnmarshalOrderFromInterface(val, &upgOrder)
	if unmErr == nil {
		assert.Equal(t, order.Id, upgOrder.Id)
		assert.True(t, upgOrder.Valid)
	} else {
		t.Fatal(unmErr.Error())
	}
}
Пример #2
0
func (b *Bartender) doHandleOrder(w http.ResponseWriter, r *http.Request, order commons.Order) {
	log.Println(fmt.Sprintf("Bartender | receive one order : %s", order))
	c, redisErr := redis.Dial("tcp", b.redisAddr)
	defer c.Close()
	if redisErr != nil {
		log.Printf("An error happends : %s \n\r", redisErr.Error())
		w.WriteHeader(500)
		return
	}
	// check the existance of the order
	exist, existErr := c.Do("EXISTS", order.Id)
	if existErr != nil {
		log.Printf("An error happends : %s \n\r", existErr.Error())
		w.WriteHeader(500)
		return
	}
	if exist.(int64) != 1 {
		log.Printf("Any order founded for order %d \n\r", order.Id)
		w.WriteHeader(404)
		return
	}
	// get the existing order
	var existingOrder commons.Order
	data, getError := c.Do("GET", order.Id)
	if getError != nil {
		log.Printf("An error happends : %s \n\r", getError.Error())
		w.WriteHeader(500)
		return
	}
	// check that the order is not already validated
	commons.UnmarshalOrderFromInterface(data, &existingOrder)
	if existingOrder.Valid {
		// already validated
		log.Printf("Order with id %d is already validated \n\r", order.Id)
		w.WriteHeader(403)
		return
	}
	// register the validated
	existingOrder.Valid = true
	bd, marshalError := json.Marshal(existingOrder)
	if marshalError != nil {
		log.Printf("An error happends : %s \n\r", marshalError.Error())
		w.WriteHeader(500)
		return
	}
	d := time.Duration(int(math.Pow(float64(order.Type+1), 2.0))*order.Quantity) * time.Millisecond * 10
	log.Println(fmt.Sprintf("Bartender | wait for %d millisecond", d))
	time.Sleep(d)
	_, saveErr := c.Do("SET", existingOrder.Id, string(bd))
	if saveErr != nil {
		log.Printf("An error happends : %s \n\r", saveErr.Error())
		w.WriteHeader(500)
		return
	}
	log.Println(fmt.Sprintf("Bartender | order %s successfully registered", order))
	w.WriteHeader(200)
}
Пример #3
0
func paymentEndPoint(w http.ResponseWriter, r *http.Request) {
	var order commons.Order
	var credit commons.Credit
	urlPart := strings.Split(r.URL.Path, "/")
	// todo test me
	if len(urlPart) < 3 {
		log.Printf("payment | An error happends. Not enought variable path. Expected 3, get %d \n\r", len(urlPart))
		w.WriteHeader(404)
		return
	}
	c, redisErr := redis.Dial("tcp", redisAddr)
	defer c.Close()
	// todo test me
	if redisErr != nil {
		log.Printf("payment | An error happends : %s \n\r", redisErr.Error())
		w.WriteHeader(500)
		return
	}
	// check the existance of the order
	exist, existErr := c.Do("EXISTS", urlPart[3])
	if existErr != nil {
		log.Printf("An error happends : %s \n\r", existErr.Error())
		w.WriteHeader(500)
		return
	}
	// todo test me
	if exist.(int64) != 1 {
		log.Printf("Any order founded for order %d \n\r", urlPart[3])
		w.WriteHeader(404)
		return
	}
	data, getError := c.Do("GET", urlPart[3])
	if getError != nil {
		log.Printf("An error happends : %s \n\r", getError.Error())
		w.WriteHeader(500)
		return
	}
	// check that the order is not already validated
	// todo test me
	commons.UnmarshalOrderFromInterface(data, &order)
	if !order.Valid {
		// not validated
		log.Printf("Order with id %d is not validated \n\r", order.Id)
		w.WriteHeader(403)
		return
	}
	c.Do("DEL", order.Id)
	// check the existance of the order
	existCredit, _ := c.Do("EXISTS", urlPart[2])
	// todo test me
	if existCredit.(int64) != 1 {
		credit = commons.Credit{PlayerId: urlPart[2], Score: computePaymentSum(order), Timestamp: int(time.Now().UTC().Unix())}
	} else {
		dataCred, _ := c.Do("GET", urlPart[2])
		commons.UnmarshallCreditFromInterface(dataCred, &credit)
		credit.Score += computePaymentSum(order)
	}

	bd, _ := json.Marshal(credit)
	_, saveErr := c.Do("SET", credit.PlayerId, string(bd))
	if saveErr != nil {
		log.Printf("An error happends : %s \n\r", saveErr.Error())
		w.WriteHeader(500)
		return
	}
	log.Println(fmt.Sprintf("payment | credit %s successfully registered", credit))
	func() {
		ch <- commons.Notification{PlayerId: credit.PlayerId, Type: commons.Score, Score: credit.Score, Rate: float64(credit.Score) / float64((int(time.Now().UTC().Unix()) - credit.Timestamp))}
	}()
	w.WriteHeader(200)
}