<commit_before>//  Copyright (c) 2015 Couchbase, 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 monitor

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"testing"
)

func TestEmptyStartMonitorCluster(t *testing.T) {
	HttpGet = func(url string) (resp *http.Response, err error) {
		t.Errorf("expected no get")
		return nil, nil
	}
	defer func() {
		HttpGet = http.Get
	}()

	opt := MonitorClusterOptions{}

	m, err := StartMonitorCluster(nil, nil, opt)
	if err != nil || m == nil {
		t.Errorf("expected no err")
	}

	m.Stop()
}

func TestStartMonitorClusterEmptyCfg(t *testing.T) {
	httpGets := 0
	HttpGet = func(url string) (resp *http.Response, err error) {
		httpGets++

		if url != "url0/api/cfg" {
			t.Errorf("expected http get, url: %s", url)
		}

		return &http.Response{
			StatusCode: 200,
			Body:       ioutil.NopCloser(bytes.NewBuffer([]byte("{}"))),
		}, nil
	}
	defer func() {
		HttpGet = http.Get
	}()

	sampleCh := make(chan MonitorSample)

	opt := MonitorClusterOptions{}

	m, err := StartMonitorCluster([]string{"url0"},
		sampleCh, opt)
	if err != nil || m == nil {
		t.Errorf("expected no err")
	}

	s, ok := <-sampleCh
	if !ok ||
		s.Kind != "/api/cfg" ||
		s.Url != "url0" ||
		s.UUID != "" ||
		s.Error != nil ||
		string(s.Data) != "{}" {
		t.Errorf("unexpected sample: %#v, ok: %v", s, ok)
	}

	m.Stop()

	select {
	case <-sampleCh:
		t.Errorf("unexpected sample")
	default:
	}

	if httpGets != 1 {
		t.Errorf("expected 1 http gets")
	}
}

func TestStartMonitorCluster(t *testing.T) {
	opt := MonitorClusterOptions{}
	samplesBeforeStop := 2
	expHttpGets := 3
	testStartMonitorCluster(t, "2x3",
		opt, samplesBeforeStop, expHttpGets, false)

	opt = MonitorClusterOptions{
		CfgSampleInterval:   10,
		MonitorNodesOptions: MonitorNodesOptions{},
	}
	samplesBeforeStop = 5
	expHttpGets = 6
	testStartMonitorCluster(t, "more cfgs",
		opt, samplesBeforeStop, expHttpGets, true)
}

func testStartMonitorCluster(t *testing.T,
	label string,
	opt MonitorClusterOptions,
	samplesBeforeStop int,
	expHttpGets int,
	cfgOk bool) {
	httpGets := 0
	HttpGet = func(url string) (resp *http.Response, err error) {
		httpGets++<commit_msg>trying to reduce race window in TestStartMonitorCluster<commit_after>//  Copyright (c) 2015 Couchbase, 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 monitor

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"testing"
)

func TestEmptyStartMonitorCluster(t *testing.T) {
	HttpGet = func(url string) (resp *http.Response, err error) {
		t.Errorf("expected no get")
		return nil, nil
	}
	defer func() {
		HttpGet = http.Get
	}()

	opt := MonitorClusterOptions{}

	m, err := StartMonitorCluster(nil, nil, opt)
	if err != nil || m == nil {
		t.Errorf("expected no err")
	}

	m.Stop()
}

func TestStartMonitorClusterEmptyCfg(t *testing.T) {
	httpGets := 0
	HttpGet = func(url string) (resp *http.Response, err error) {
		httpGets++

		if url != "url0/api/cfg" {
			t.Errorf("expected http get, url: %s", url)
		}

		return &http.Response{
			StatusCode: 200,
			Body:       ioutil.NopCloser(bytes.NewBuffer([]byte("{}"))),
		}, nil
	}
	defer func() {
		HttpGet = http.Get
	}()

	sampleCh := make(chan MonitorSample)

	opt := MonitorClusterOptions{}

	m, err := StartMonitorCluster([]string{"url0"},
		sampleCh, opt)
	if err != nil || m == nil {
		t.Errorf("expected no err")
	}

	s, ok := <-sampleCh
	if !ok ||
		s.Kind != "/api/cfg" ||
		s.Url != "url0" ||
		s.UUID != "" ||
		s.Error != nil ||
		string(s.Data) != "{}" {
		t.Errorf("unexpected sample: %#v, ok: %v", s, ok)
	}

	m.Stop()

	select {
	case <-sampleCh:
		t.Errorf("unexpected sample")
	default:
	}

	if httpGets != 1 {
		t.Errorf("expected 1 http gets")
	}
}

func TestStartMonitorCluster(t *testing.T) {
	opt := MonitorClusterOptions{}
	samplesBeforeStop := 2
	expHttpGets := 3
	testStartMonitorCluster(t, "2x3",
		opt, samplesBeforeStop, expHttpGets, false)

	opt = MonitorClusterOptions{
		CfgSampleInterval:   1,
		MonitorNodesOptions: MonitorNodesOptions{},
	}
	samplesBeforeStop = 5
	expHttpGets = 6
	testStartMonitorCluster(t, "more cfgs",
		opt, samplesBeforeStop, expHttpGets, true)
}

func testStartMonitorCluster(t *testing.T,
	label string,
	opt MonitorClusterOptions,
	samplesBeforeStop int,
	expHttpGets int,
	cfgOk bool) {
	httpGets := 0
	HttpGet = func(url string) (resp *http.Response, err error) {
		httpGets++
{'fraction_non_alphanumeric': 0.1385329619312906, 'fraction_numerical': 0.00947075208913649, 'mean_word_length': 3.762157382847038, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '7221333', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1850, 'n_words': 541}