<commit_before>package device

import (
	"bytes"
	"encoding/json"
	"errors"
	"github.com/Comcast/webpa-common/logging"
	"github.com/Comcast/webpa-common/wrp"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func testMessageHandlerLogger(t *testing.T) {
	var (
		assert = assert.New(t)
		logger = logging.TestLogger(t)<commit_msg>added tests for the Timeout constructor<commit_after>package device

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"github.com/Comcast/webpa-common/logging"
	"github.com/Comcast/webpa-common/wrp"
	"github.com/justinas/alice"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
)

func testTimeout(o *Options, t *testing.T) {
	var (
		assert   = assert.New(t)
		request  = httptest.NewRequest("GET", "/", nil)
		response = httptest.NewRecorder()
		ctx      context.Context

		handler = alice.New(Timeout(o)).Then(
			http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
				ctx = request.Context()
				assert.NotEqual(context.Background(), ctx)

				deadline, ok := ctx.Deadline()
				assert.False(deadline.IsZero())
				assert.True(deadline.Sub(time.Now()) <= o.requestTimeout())
				assert.True(ok)
			}),
		)
	)

	handler.ServeHTTP(response, request)

	select {
	case <-ctx.Done():
		// pass
	default:
		assert.Fail("The context should have been cancelled after ServeHTTP exits")
	}
}

func TestTimeout(t *testing.T) {
	t.Run(
		"NilOptions",
		func(t *testing.T) { testTimeout(nil, t) },
	)

	t.Run(
		"DefaultOptions",
		func(t *testing.T) { testTimeout(new(Options), t) },
	)

	t.Run(
		"CustomOptions",
		func(t *testing.T) { testTimeout(&Options{RequestTimeout: 17 * time.Second}, t) },
	)
}

func testMessageHandlerLogger(t *testing.T) {
	var (
		assert = assert.New(t)
		logger = logging.TestLogger(t)
{'fraction_non_alphanumeric': 0.1594488188976378, 'fraction_numerical': 0.000984251968503937, 'mean_word_length': 4.146835443037975, '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': '6851367', 'n_tokens_mistral': 827, 'n_tokens_neox': 780, 'n_words': 125}