<commit_before>package agent

import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	docker "github.com/fsouza/go-dockerclient"
	"github.com/hashicorp/consul/api"
	"github.com/hashicorp/consul/command/agent/mock"
	"github.com/hashicorp/consul/testutil/retry"
	"github.com/hashicorp/consul/types"
)

func expectStatus(t *testing.T, script, status string) {
	notif := mock.NewNotify()
	check := &CheckMonitor{
		Notify:   notif,
		CheckID:  types.CheckID("foo"),
		Script:   script,
		Interval: 10 * time.Millisecond,
		Logger:   log.New(os.Stderr, UniqueID(), log.LstdFlags),
	}
	check.Start()
	defer check.Stop()
	retry.Run(t, func(r *retry.R) {
		if got, want := notif.Updates("foo"), 2; got < want {
			r.Fatalf("got %d updates want at least %d", got, want)
		}
		if got, want := notif.State("foo"), status; got != want {
			r.Fatalf("got state %q want %q", got, want)
		}
	})
}

func TestCheckMonitor_Passing(t *testing.T) {
	t.Parallel()
	expectStatus(t, "exit 0", api.HealthPassing)
}

func TestCheckMonitor_Warning(t *testing.T) {
	t.Parallel()
	expectStatus(t, "exit 1", api.HealthWarning)
}

func TestCheckMonitor_Critical(t *testing.T) {
	t.Parallel()
	expectStatus(t, "exit 2", api.HealthCritical)
}

func TestCheckMonitor_BadCmd(t *testing.T) {
	t.Parallel()
	expectStatus(t, "foobarbaz", api.HealthCritical)
}

func TestCheckMonitor_Timeout(t *testing.T) {
	t.Parallel()
	notif := mock.NewNotify()
	check := &CheckMonitor{
		Notify:   notif,
		CheckID:  types.CheckID("foo"),
		Script:   "sleep 1 && exit 0",
		Interval: 10 * time.Millisecond,
		Timeout:  5 * time.Millisecond,
		Logger:   log.New(os.Stderr, UniqueID(), log.LstdFlags),
	}
	check.Start()
	defer check.Stop()

	time.Sleep(50 * time.Millisecond)

	// Should have at least 2 updates
	if notif.Updates("foo") < 2 {
		t.Fatalf("should have at least 2 updates %v", notif.UpdatesMap())
	}
	if notif.State("foo") != "critical" {
		t.Fatalf("should be critical %v", notif.StateMap())
	}
}

func TestCheckMonitor_RandomStagger(t *testing.T) {
	t.Parallel()
	notif := mock.NewNotify()
	check := &CheckMonitor{
		Notify:   notif,
		CheckID:  types.CheckID("foo"),
		Script:   "exit 0",
		Interval: 25 * time.Millisecond,
		Logger:   log.New(os.Stderr, UniqueID(), log.LstdFlags),
	}
	check.Start()
	defer check.Stop()

	time.Sleep(50 * time.Millisecond)

	// Should have at least 1 update
	if notif.Updates("foo") < 1 {
		t.Fatalf("should have 1 or more updates %v", notif.UpdatesMap())
	}
<commit_msg>test: increase sleep time for test<commit_after>package agent

import (
	"bytes"
	"errors"
	"fmt"
	"log"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"os/exec"
	"strings"
	"testing"
	"time"

	docker "github.com/fsouza/go-dockerclient"
	"github.com/hashicorp/consul/api"
	"github.com/hashicorp/consul/command/agent/mock"
	"github.com/hashicorp/consul/testutil/retry"
	"github.com/hashicorp/consul/types"
)

func expectStatus(t *testing.T, script, status string) {
	notif := mock.NewNotify()
	check := &CheckMonitor{
		Notify:   notif,
		CheckID:  types.CheckID("foo"),
		Script:   script,
		Interval: 10 * time.Millisecond,
		Logger:   log.New(os.Stderr, UniqueID(), log.LstdFlags),
	}
	check.Start()
	defer check.Stop()
	retry.Run(t, func(r *retry.R) {
		if got, want := notif.Updates("foo"), 2; got < want {
			r.Fatalf("got %d updates want at least %d", got, want)
		}
		if got, want := notif.State("foo"), status; got != want {
			r.Fatalf("got state %q want %q", got, want)
		}
	})
}

func TestCheckMonitor_Passing(t *testing.T) {
	t.Parallel()
	expectStatus(t, "exit 0", api.HealthPassing)
}

func TestCheckMonitor_Warning(t *testing.T) {
	t.Parallel()
	expectStatus(t, "exit 1", api.HealthWarning)
}

func TestCheckMonitor_Critical(t *testing.T) {
	t.Parallel()
	expectStatus(t, "exit 2", api.HealthCritical)
}

func TestCheckMonitor_BadCmd(t *testing.T) {
	t.Parallel()
	expectStatus(t, "foobarbaz", api.HealthCritical)
}

func TestCheckMonitor_Timeout(t *testing.T) {
	t.Parallel()
	notif := mock.NewNotify()
	check := &CheckMonitor{
		Notify:   notif,
		CheckID:  types.CheckID("foo"),
		Script:   "sleep 1 && exit 0",
		Interval: 10 * time.Millisecond,
		Timeout:  5 * time.Millisecond,
		Logger:   log.New(os.Stderr, UniqueID(), log.LstdFlags),
	}
	check.Start()
	defer check.Stop()

	time.Sleep(150 * time.Millisecond)

	// Should have at least 2 updates
	if notif.Updates("foo") < 2 {
		t.Fatalf("should have at least 2 updates %v", notif.UpdatesMap())
	}
	if notif.State("foo") != "critical" {
		t.Fatalf("should be critical %v", notif.StateMap())
	}
}

func TestCheckMonitor_RandomStagger(t *testing.T) {
	t.Parallel()
	notif := mock.NewNotify()
	check := &CheckMonitor{
		Notify:   notif,
		CheckID:  types.CheckID("foo"),
		Script:   "exit 0",
		Interval: 25 * time.Millisecond,
		Logger:   log.New(os.Stderr, UniqueID(), log.LstdFlags),
	}
	check.Start()
	defer check.Stop()

	time.Sleep(50 * time.Millisecond)

	// Should have at least 1 update
	if notif.Updates("foo") < 1 {
		t.Fatalf("should have 1 or more updates %v", notif.UpdatesMap())
	}

{'fraction_non_alphanumeric': 0.16334115805946792, 'fraction_numerical': 0.00958528951486698, 'mean_word_length': 4.113, 'pattern_counts': {'":': 0, '<': 9, '<?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': '785966', 'n_tokens_mistral': 2098, 'n_tokens_neox': 1988, 'n_words': 429}