<commit_before>// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package outboundtest

import (
	"bytes"
	"context"
	"io/ioutil"
	"time"

	"go.uber.org/yarpc/api/transport"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/atomic"
)

// OutboundEventCallable is an object that can be used in conjunction with a
// yarpctest.FakeOutbound to create Fake functionality for an outbound through
// OutboundEvents.
// Every call to `OutboundEventCallable.Call` will be sent to an event in the
// OutboundEventCallable's event list.  We will atomically increment the event
// index every time a request is sent, and we will fail the test if too many or
// too few requests were called.
type OutboundEventCallable struct {
	t      require.TestingT
	events []*OutboundEvent
	index  *atomic.Int32
}

// NewOutboundEventCallable sets up an OutboundEventCallable.
func NewOutboundEventCallable(t require.TestingT, events []*OutboundEvent) *OutboundEventCallable {
	return &OutboundEventCallable{
		t:      t,
		events: events,
		index:  atomic.NewInt32(0),
	}
}

// Call implements the yarpctest.OutboundCallable function signature.  It will
// send all requests to the appropriate event registered in the event slice.
func (c *OutboundEventCallable) Call(ctx context.Context, req *transport.Request) (*transport.Response, error) {
	eventIndex := c.index.Inc() - 1
	require.True(c.t, int(eventIndex) < len(c.events), "attempted to execute event #%d on the outbound, there are only %d events", eventIndex+1, len(c.events))
	return c.events[eventIndex].Call(ctx, c.t, req)
}

// Cleanup validates that all events in the OutboundEventCallable were
// called before we finished the test.
func (c *OutboundEventCallable) Cleanup() {
	assert.Equal(
		c.t,
		int(c.index.Load()),
		len(c.events),
		"did not execute the proper number of outbound calls",
	)
}

// OutboundEvent is a struct to validate an individual call to an outbound.
// It has explicit checks for all request and timeout attributes, and it
// can return all the information needed from the request.
type OutboundEvent struct {
	// context.Deadline validation
	WantTimeout       time.Duration
	WantTimeoutBounds time.Duration

	// transport.Request validation
	WantCaller          string
	WantService         string
	WantEncoding        transport.Encoding
	WantProcedure       string
	WantShardKey        string
	WantRoutingKey      string
	WantRoutingDelegate string
	WantHeaders         transport.Headers

	// WantBody validates the request's body
	// It is special because if it is not set, we will not exhaust the
	// request body's io.Reader, which could cause an error up the stack,
	// this is by design for edge test cases.
	WantBody string

	// Indicates that we should block until the context is `Done`
	WaitForTimeout bool

	// Attributes put into the transport.Response object.
	GiveError            error
	GiveApplicationError bool
	GiveRespHeaders      transport.Headers
	GiveRespBody         string
}

// Call will validate a single call to the outbound event based on
// the OutboundEvent's parameters.
func (e *OutboundEvent) Call(ctx context.Context, t require.TestingT, req *transport.Request) (*transport.Response, error) {
	if e.WantTimeout != 0 {
		timeoutBounds := e.WantTimeoutBounds
		if timeoutBounds == 0 {
			timeoutBounds = time.Millisecond * 10
		}
		deadline, ok := ctx.Deadline()
		require.True(t, ok, "wanted context deadline, but there was no deadline")<commit_msg>Increase default bounds for validating OutboundEvent timeouts (#1105)<commit_after>// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package outboundtest

import (
	"bytes"
	"context"
	"io/ioutil"
	"time"

	"go.uber.org/yarpc/api/transport"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.uber.org/atomic"
)

// OutboundEventCallable is an object that can be used in conjunction with a
// yarpctest.FakeOutbound to create Fake functionality for an outbound through
// OutboundEvents.
// Every call to `OutboundEventCallable.Call` will be sent to an event in the
// OutboundEventCallable's event list.  We will atomically increment the event
// index every time a request is sent, and we will fail the test if too many or
// too few requests were called.
type OutboundEventCallable struct {
	t      require.TestingT
	events []*OutboundEvent
	index  *atomic.Int32
}

// NewOutboundEventCallable sets up an OutboundEventCallable.
func NewOutboundEventCallable(t require.TestingT, events []*OutboundEvent) *OutboundEventCallable {
	return &OutboundEventCallable{
		t:      t,
		events: events,
		index:  atomic.NewInt32(0),
	}
}

// Call implements the yarpctest.OutboundCallable function signature.  It will
// send all requests to the appropriate event registered in the event slice.
func (c *OutboundEventCallable) Call(ctx context.Context, req *transport.Request) (*transport.Response, error) {
	eventIndex := c.index.Inc() - 1
	require.True(c.t, int(eventIndex) < len(c.events), "attempted to execute event #%d on the outbound, there are only %d events", eventIndex+1, len(c.events))
	return c.events[eventIndex].Call(ctx, c.t, req)
}

// Cleanup validates that all events in the OutboundEventCallable were
// called before we finished the test.
func (c *OutboundEventCallable) Cleanup() {
	assert.Equal(
		c.t,
		int(c.index.Load()),
		len(c.events),
		"did not execute the proper number of outbound calls",
	)
}

// OutboundEvent is a struct to validate an individual call to an outbound.
// It has explicit checks for all request and timeout attributes, and it
// can return all the information needed from the request.
type OutboundEvent struct {
	// context.Deadline validation
	WantTimeout       time.Duration
	WantTimeoutBounds time.Duration

	// transport.Request validation
	WantCaller          string
	WantService         string
	WantEncoding        transport.Encoding
	WantProcedure       string
	WantShardKey        string
	WantRoutingKey      string
	WantRoutingDelegate string
	WantHeaders         transport.Headers

	// WantBody validates the request's body
	// It is special because if it is not set, we will not exhaust the
	// request body's io.Reader, which could cause an error up the stack,
	// this is by design for edge test cases.
	WantBody string

	// Indicates that we should block until the context is `Done`
	WaitForTimeout bool

	// Attributes put into the transport.Response object.
	GiveError            error
	GiveApplicationError bool
	GiveRespHeaders      transport.Headers
	GiveRespBody         string
}

// Call will validate a single call to the outbound event based on
// the OutboundEvent's parameters.
func (e *OutboundEvent) Call(ctx context.Context, t require.TestingT, req *transport.Request) (*transport.Response, error) {
	if e.WantTimeout != 0 {
		timeoutBounds := e.WantTimeoutBounds
		if timeoutBounds == 0 {
			timeoutBounds = time.Millisecond * 20
		}
		deadline, ok := ctx.Deadline()
		require.True(t, ok, "wanted context deadline, but there was no deadline")
{'fraction_non_alphanumeric': 0.07854030501089325, 'fraction_numerical': 0.003703703703703704, 'mean_word_length': 3.8270241850683493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4816923', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2508, 'n_words': 1076}