func TestOrderMaker(t *testing.T) {
	url := "http://calback:4444"
	wg := new(sync.WaitGroup)
	wg.Add(1)
	mux := http.NewServeMux()
	mux.HandleFunc("/orders", func(rw http.ResponseWriter, rq *http.Request) {
		assert.Equal(t, http.MethodPost, rq.Method)

		var order commons.Order
		commons.UnmarshalOrderFromHttp(rq, &order)

		c, _ := redis.Dial("tcp", "192.168.99.100:6379")
		defer c.Close()
		exist, _ := c.Do("EXISTS", order.Id)
		assert.Equal(t, int64(1), exist.(int64))
		assert.True(t, strings.HasPrefix(order.CallBackUrl, url+"/bill/playerId/"))
		wg.Done()
		rw.WriteHeader(200)
	})
	srv := httptest.NewServer(mux)
	unReg := make(chan commons.Registration)
	reg := commons.Registration{PlayerId: "playerId", Ip: srv.URL}

	startNewOrderMaker(url, "192.168.99.100:6379", reg, unReg)
	wg.Wait()
}
func Test_unmarshallOrder_should_unmarshal_with_error(t *testing.T) {
	// given
	order := new(commons.Order)
	var req http.Request
	req.Body = nopCloser{bytes.NewBuffer(make([]byte, 0))}
	req.ContentLength = int64(0)
	// when
	err := commons.UnmarshalOrderFromHttp(&req, order)
	assert.NotNil(t, err)
}
func Test_unmarshallOrder_should_unmarshal_without_error(t *testing.T) {
	// given
	expectedOrder := commons.Order{Id: 1, Quantity: 5, Type: commons.Beer, CallBackUrl: "http://callback.com/money"}
	order := new(commons.Order)
	body, _ := json.Marshal(expectedOrder)
	var req http.Request
	req.Body = nopCloser{bytes.NewBuffer(body)}
	req.ContentLength = int64(len(body))
	// when
	err := commons.UnmarshalOrderFromHttp(&req, order)
	assert.Nil(t, err)
	assert.Equal(t, expectedOrder, *order)
}
func (b *Bartender) handleOrder(w http.ResponseWriter, r *http.Request) {
	var order commons.Order
	unMarshallErr := commons.UnmarshalOrderFromHttp(r, &order)
	if unMarshallErr != nil {
		log.Printf("An error happends : %s \n\r", unMarshallErr.Error())
		return
	}
	// try to get one token for the
	req := tokenReq{playerId: order.PlayerId, res: make(chan chan bool)}
	b.tokenChan <- req
	c := <-req.res
	select {
	case token := <-c:
		// one token is available
		defer func() {
			c <- token
		}()
		b.doHandleOrder(w, r, order)
	default:
		b.doApplyPenalty(w, r, order)
	}

}