<commit_before>// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package timeouts

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/coreos/ignition/tests/register"
	"github.com/coreos/ignition/tests/types"
)

func init() {
	register.Register(register.PositiveTest, IncreaseHTTPResponseHeadersTimeout())
	register.Register(register.PositiveTest, ConfirmHTTPBackoffWorks())
}

var (
	respondDelayServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Hold the connection open for 11 seconds, then return
		time.Sleep(time.Second * 11)
		w.WriteHeader(http.StatusOK)
	}))

	lastResponses          = map[string]time.Time{}
	respondThrottledServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		lastResponse, ok := lastResponses[r.RequestURI]
		if ok && time.Since(lastResponse) > time.Second*4 {
			// Only respond successfully if it's been more than 4 seconds since
			// the last attempt
			w.WriteHeader(http.StatusOK)
			return
		}
		lastResponses[r.RequestURI] = time.Now()
		w.WriteHeader(http.StatusInternalServerError)
	}))
)

func IncreaseHTTPResponseHeadersTimeout() types.Test {
	name := "Increase HTTP Response Headers Timeout"<commit_msg>tests/timeouts: fix concurrency panic<commit_after>// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package timeouts

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"sync"
	"time"

	"github.com/coreos/ignition/tests/register"
	"github.com/coreos/ignition/tests/types"
)

func init() {
	register.Register(register.PositiveTest, IncreaseHTTPResponseHeadersTimeout())
	register.Register(register.PositiveTest, ConfirmHTTPBackoffWorks())
}

var (
	respondDelayServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Hold the connection open for 11 seconds, then return
		time.Sleep(time.Second * 11)
		w.WriteHeader(http.StatusOK)
	}))

	lastResponses          = map[string]time.Time{}
	lastResponsesLock      = sync.Mutex{}
	respondThrottledServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var status int
		lastResponsesLock.Lock()
		lastResponse, ok := lastResponses[r.RequestURI]
		if ok && time.Since(lastResponse) > time.Second*4 {
			// Only respond successfully if it's been more than 4 seconds since
			// the last attempt
			status = http.StatusOK
		} else {
			status = http.StatusInternalServerError
			lastResponses[r.RequestURI] = time.Now()
		}
		lastResponsesLock.Unlock()
		w.WriteHeader(status)
	}))
)

func IncreaseHTTPResponseHeadersTimeout() types.Test {
	name := "Increase HTTP Response Headers Timeout"
{'fraction_non_alphanumeric': 0.10772520716385993, 'fraction_numerical': 0.007484629778134189, 'mean_word_length': 4.1900138696255205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5983631', 'n_tokens_mistral': 1190, 'n_tokens_neox': 1115, 'n_words': 329}