<commit_before>package ggping

import (
	"runtime"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
)

func TestControllerInjection(t *testing.T) {
	c := newController()
	Convey("Given a controller created by internal function newController", t, func() {
		Convey("When retrieving a controller sc from Ctl()", func() {
			sc := Ctl()
			Convey("Then c should NOT equals sc", func() {
				So(c, ShouldNotEqual, sc)
			})
		})
		Convey("When invoking SetCtl(c)", func() {
			SetCtl(c)
			Convey("When retrieving a controller sc from Ctl()", func() {
				sc := Ctl()
				Convey("Then c should equals sc", func() {
					So(c, ShouldEqual, sc)
				})
			})
		})
	})
}

func TestControllerOSPingerRegistration(t *testing.T) {
	type ospinger struct {
		OSPinger
	}
	type row struct {
		ver  uint8
		prot uint8
		goos string
		osp  *ospinger
	}
	table := []*row{
		{IPV4, IPProtoICMP, "linux", new(ospinger)},
		{IPV4, IPProtoUDP, "linux", new(ospinger)},
		{IPV4, IPProtoTCP, "linux", new(ospinger)},
		{IPV6, IPProtoICMP, "linux", new(ospinger)},
		{IPV6, IPProtoUDP, "linux", new(ospinger)},
		{IPV6, IPProtoTCP, "linux", new(ospinger)},
		{IPV4, IPProtoICMP, "darwin", new(ospinger)},
		{IPV4, IPProtoUDP, "darwin", new(ospinger)},
		{IPV4, IPProtoTCP, "darwin", new(ospinger)},
		{IPV6, IPProtoICMP, "darwin", new(ospinger)},
		{IPV6, IPProtoUDP, "darwin", new(ospinger)},
		{IPV6, IPProtoTCP, "darwin", new(ospinger)},
		{IPV4, IPProtoICMP, "windows", new(ospinger)},
		{IPV4, IPProtoUDP, "windows", new(ospinger)},
		{IPV4, IPProtoTCP, "windows", new(ospinger)},
		{IPV6, IPProtoICMP, "windows", new(ospinger)},
		{IPV6, IPProtoUDP, "windows", new(ospinger)},
		{IPV6, IPProtoTCP, "windows", new(ospinger)},
		{IPV4, IPProtoICMP, "freebsd", new(ospinger)},
		{IPV4, IPProtoUDP, "freebsd", new(ospinger)},
		{IPV4, IPProtoTCP, "freebsd", new(ospinger)},
		{IPV6, IPProtoICMP, "freebsd", new(ospinger)},
		{IPV6, IPProtoUDP, "freebsd", new(ospinger)},
		{IPV6, IPProtoTCP, "freebsd", new(ospinger)},
	}

	defer traceMem("TestOSPingerRegistration", statsNow())
	Convey("Given a controller c retrieved by singleton function Ctl()", t, func() {
		c := Ctl()
		Convey("When a controller sc is retrieved by sigleton function Ctl()", func() {
			sc := Ctl()
			Convey("Then sc should be equal c", func() {
				So(sc, ShouldEqual, c)
			})
		})
		Convey("Given a list of  ospinger to register", func() {
			Convey("When calling c.ResgisterOSPinger() for each of them", func() {
				Convey("Then returned error should be nil and returned ospinger should be equals ospinger", func() {
					for _, r := range table {
						ospr, err := c.RegisterOSPinger(r.ver, r.prot, r.goos, r.osp)
						So(err, ShouldBeNil)
						So(ospr, ShouldEqual, r.osp)
					}
				})
			})
		})
		Convey("Given a list of ospinger already registered", func() {
			Convey("When calling c.OSPinger() for each of them", func() {
				Convey("Then returned error should be nil and returned ospinger should be equals ospinger", func() {
					for _, r := range table {
						ospr, err := c.OSPinger(r.ver, r.prot, r.goos)
						So(err, ShouldBeNil)
						So(ospr, ShouldEqual, r.osp)
					}
				})
			})
		})
		Convey("Given a list of ospinger already registered and retrieved", func() {
			Convey("When calling c.UnRegisterOSPinger()", func() {
				Convey("Then returned error should be nil and returned ospinger should be equals ospinger", func() {
					for _, r := range table {
						ospr, err := c.UnRegisterOSPinger(r.ver, r.prot, r.goos)
						So(err, ShouldBeNil)
						So(ospr, ShouldEqual, r.osp)
					}
				})
			})
		})
		Convey("Given a list of ospinger already deleted", func() {
			Convey("When calling c.OSPinger()", func() {
				Convey("Then returned error should NOT be nil and returned ospinger should be nil", func() {
					for _, r := range table {
						ospr, err := c.OSPinger(r.ver, r.prot, r.goos)
						So(err, ShouldNotBeNil)
						So(ospr, ShouldBeNil)
					}
				})
			})
		})
	})
}

type fakeospinger struct {
	timeToReturn time.Time
}

func (osp *fakeospinger) Ping(r Requester) time.Time {
	return osp.timeToReturn
}

func TestControllerPing(t *testing.T) {
	Convey("Given a fake OSPinger with the Ping function implemented", t, func() {
		osp := new(fakeospinger)
		Convey("When registering it to the  controller", func() {
			Ctl().RegisterOSPinger(IPV4, IPProtoICMP, runtime.GOOS, osp)
			Convey("Then the controller should have it registered", func() {
				ospr, err := Ctl().OSPinger(IPV4, IPProtoICMP, runtime.GOOS)
				So(err, ShouldBeNil)
				So(ospr, ShouldEqual, osp)
			})
		})
	})
	type testRequest struct {
		trequest
		failBefore bool
	}

	Convey("Given a list of Requests and expected behaviours", t, func() {
		Convey("When calling the controller Ping function for each one of those", func() {<commit_msg>Design Phase<commit_after>package ggping

import (
	"runtime"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
)

func TestControllerInjection(t *testing.T) {
	c := newController()
	Convey("Given a controller created by internal function newController", t, func() {
		Convey("When retrieving a controller sc from Ctl()", func() {
			sc := Ctl()
			Convey("Then c should NOT equals sc", func() {
				So(c, ShouldNotEqual, sc)
			})
		})
		Convey("When invoking SetCtl(c)", func() {
			SetCtl(c)
			Convey("When retrieving a controller sc from Ctl()", func() {
				sc := Ctl()
				Convey("Then c should equals sc", func() {
					So(c, ShouldEqual, sc)
				})
			})
		})
	})
}

func TestControllerOSPingerRegistration(t *testing.T) {
	type ospinger struct {
		OSPinger
	}
	type row struct {
		ver  uint8
		prot uint8
		goos string
		osp  *ospinger
	}
	table := []*row{
		{IPV4, IPProtoICMP, "linux", new(ospinger)},
		{IPV4, IPProtoUDP, "linux", new(ospinger)},
		{IPV4, IPProtoTCP, "linux", new(ospinger)},
		{IPV6, IPProtoICMP, "linux", new(ospinger)},
		{IPV6, IPProtoUDP, "linux", new(ospinger)},
		{IPV6, IPProtoTCP, "linux", new(ospinger)},
		{IPV4, IPProtoICMP, "darwin", new(ospinger)},
		{IPV4, IPProtoUDP, "darwin", new(ospinger)},
		{IPV4, IPProtoTCP, "darwin", new(ospinger)},
		{IPV6, IPProtoICMP, "darwin", new(ospinger)},
		{IPV6, IPProtoUDP, "darwin", new(ospinger)},
		{IPV6, IPProtoTCP, "darwin", new(ospinger)},
		{IPV4, IPProtoICMP, "windows", new(ospinger)},
		{IPV4, IPProtoUDP, "windows", new(ospinger)},
		{IPV4, IPProtoTCP, "windows", new(ospinger)},
		{IPV6, IPProtoICMP, "windows", new(ospinger)},
		{IPV6, IPProtoUDP, "windows", new(ospinger)},
		{IPV6, IPProtoTCP, "windows", new(ospinger)},
		{IPV4, IPProtoICMP, "freebsd", new(ospinger)},
		{IPV4, IPProtoUDP, "freebsd", new(ospinger)},
		{IPV4, IPProtoTCP, "freebsd", new(ospinger)},
		{IPV6, IPProtoICMP, "freebsd", new(ospinger)},
		{IPV6, IPProtoUDP, "freebsd", new(ospinger)},
		{IPV6, IPProtoTCP, "freebsd", new(ospinger)},
	}

	defer traceMem("TestOSPingerRegistration", statsNow())
	Convey("Given a controller c retrieved by singleton function Ctl()", t, func() {
		c := Ctl()
		Convey("When a controller sc is retrieved by sigleton function Ctl()", func() {
			sc := Ctl()
			Convey("Then sc should be equal c", func() {
				So(sc, ShouldEqual, c)
			})
		})
		Convey("Given a list of  ospinger to register", func() {
			Convey("When calling c.ResgisterOSPinger() for each of them", func() {
				Convey("Then returned error should be nil and returned ospinger should be equals ospinger", func() {
					for _, r := range table {
						ospr, err := c.RegisterOSPinger(r.ver, r.prot, r.goos, r.osp)
						So(err, ShouldBeNil)
						So(ospr, ShouldEqual, r.osp)
					}
				})
			})
		})
		Convey("Given a list of ospinger already registered", func() {
			Convey("When calling c.OSPinger() for each of them", func() {
				Convey("Then returned error should be nil and returned ospinger should be equals ospinger", func() {
					for _, r := range table {
						ospr, err := c.OSPinger(r.ver, r.prot, r.goos)
						So(err, ShouldBeNil)
						So(ospr, ShouldEqual, r.osp)
					}
				})
			})
		})
		Convey("Given a list of ospinger already registered and retrieved", func() {
			Convey("When calling c.UnRegisterOSPinger()", func() {
				Convey("Then returned error should be nil and returned ospinger should be equals ospinger", func() {
					for _, r := range table {
						ospr, err := c.UnRegisterOSPinger(r.ver, r.prot, r.goos)
						So(err, ShouldBeNil)
						So(ospr, ShouldEqual, r.osp)
					}
				})
			})
		})
		Convey("Given a list of ospinger already deleted", func() {
			Convey("When calling c.OSPinger()", func() {
				Convey("Then returned error should NOT be nil and returned ospinger should be nil", func() {
					for _, r := range table {
						ospr, err := c.OSPinger(r.ver, r.prot, r.goos)
						So(err, ShouldNotBeNil)
						So(ospr, ShouldBeNil)
					}
				})
			})
		})
	})
}

type fakeospinger struct {
	timeToReturn time.Time
}

func (osp *fakeospinger) Ping(r Requester) time.Time {
	return osp.timeToReturn
}

type testRequest struct {
	failBefore bool
	trequest
}

var testRequestTable = []testRequest{
	{false, trequest{
		target: new(target),
	}},
}

func TestControllerPing(t *testing.T) {
	Convey("Given a fake OSPinger with the Ping function implemented", t, func() {
		osp := new(fakeospinger)
		Convey("When registering it to the  controller", func() {
			Ctl().RegisterOSPinger(IPV4, IPProtoICMP, runtime.GOOS, osp)
			Convey("Then the controller should have it registered", func() {
				ospr, err := Ctl().OSPinger(IPV4, IPProtoICMP, runtime.GOOS)
				So(err, ShouldBeNil)
				So(ospr, ShouldEqual, osp)
			})
		})
	})

	Convey("Given a list of Requests and expected behaviours", t, func() {
		Convey("When calling the controller Ping function for each one of those", func() {
{'fraction_non_alphanumeric': 0.15047247329498767, 'fraction_numerical': 0.005751848808545604, 'mean_word_length': 3.8880522088353415, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3086329', 'n_tokens_mistral': 4078, 'n_tokens_neox': 3450, 'n_words': 962}