Ejemplo n.º 1
0
func Test011TranslationOfArraysWorks(t *testing.T) {

	cv.Convey(`Given a parent Snoopy struct that has an array of`+
		` concrete types, these should be translated from Sexp`+
		` correctly.`, t, func() {

		env := NewGlisp()
		defer env.parser.Stop()

		env.StandardSetup()

		x, err := env.EvalString(`
(def snoop (snoopy pack:[8 9 4]))
`)
		panicOn(err)

		cv.So(x.SexpString(), cv.ShouldEqual, ` (snoopy pack:[8 9 4])`)

		var sn Snoopy
		_, err = SexpToGoStructs(x, &sn, env)
		panicOn(err)
		VPrintf("\n sn = %#v\n", sn)
		cv.So(&sn, cv.ShouldResemble, &Snoopy{Pack: []int{8, 9, 4}})
	})
}
Ejemplo n.º 2
0
func Test004SingleSlice(t *testing.T) {

	cv.Convey("Given a parsable golang source file with type Vector struct { M []int }", t, func() {
		cv.Convey("then we should generate translation utility methods for List(Int64) <-> []int in the capnp output", func() {

			ex0 := `
type Vector struct {
  M []int
}`
			cv.So(ExtractString2String(ex0), ShouldContainModuloWhiteSpace, `

func Int64ListToSliceInt(p capn.Int64List) []int {
	v := make([]int, p.Len())
	for i := range v {
		v[i] = int(p.At(i))
	}
	return v
}
`)

			cv.So(ExtractString2String(ex0), ShouldContainModuloWhiteSpace, `
func SliceIntToInt64List(seg *capn.Segment, m []int) capn.Int64List {
	lst := seg.NewInt64List(len(m))
	for i := range m {
		lst.Set(i, int64(m[i]))
	}
	return lst
}

`)
		})
	})
}
Ejemplo n.º 3
0
func confirmOneJobRunningOneWaiting(cfg *Config) {
	snapmap := HelperSnapmap(cfg)
	running, ok := snapmap["runQlen"]
	if !ok {
		panic(fmt.Sprintf("server stat must include runQlen"))
	}
	irunning, err := strconv.Atoi(running)
	if err != nil {
		panic(err)
	}
	fmt.Printf("\n    We should see one running job now, and we see: %d. snapmap: %#v\n", irunning, snapmap)
	cv.So(irunning, cv.ShouldEqual, 1)

	waiting, ok := snapmap["waitingJobs"]
	if !ok {
		panic(fmt.Sprintf("server stat must include waitingJobs"))
	}
	iwaiting, err := strconv.Atoi(waiting)
	if err != nil {
		panic(err)
	}
	fmt.Printf("\n    We should see one waiting job now, and we see: %d. snapmap: %#v\n", iwaiting, snapmap)
	cv.So(iwaiting, cv.ShouldEqual, 1)

}
Ejemplo n.º 4
0
func TestEnvCannotContainKey(t *testing.T) {
	cv.Convey("To avoid transmitting the clusterid, the Env sent to the shepard/worker cannot contain COG_ variables or the clusterid", t, func() {
		cv.Convey("The 8 GOQ env var should all be subtracted by GetNonGOQEnv(), as well as any variable that has the specified cid in it", func() {

			// *** universal test cfg setup
			skipbye := false
			cfg := NewTestConfig()
			defer cfg.ByeTestConfig(&skipbye)
			// *** end universal test setup

			e := make(map[string]string)
			cfg.InjectConfigIntoMap(&e)

			e["UNTOUCHED"] = "sane"
			randomCid := RandomClusterId()
			e["SHALLNOTPASS"] = randomCid

			env2 := MapToEnv(e)

			cv.So(len(env2), cv.ShouldEqual, 11) // the 8 from cfg + UNTOUCHED and SHALLNOTPASS
			res := GetNonGOQEnv(env2, randomCid)

			cv.So(len(res), cv.ShouldEqual, 1)
			cv.So(res[0], cv.ShouldEqual, "UNTOUCHED=sane")
		})
	})
}
Ejemplo n.º 5
0
func Test003WriteReadThroughGeneratedTranslationCode(t *testing.T) {

	tdir := NewTempDir()
	// comment the defer out to debug any rw test failures.
	defer tdir.Cleanup()

	err := exec.Command("cp", "rw.go.txt", tdir.DirPath+"/rw.go").Run()
	if err != nil {
		fmt.Printf("cp rw.go.txt %s/rw.go failed: '%s'\n", tdir.DirPath, err)
		panic(err)
	}

	MainArgs([]string{os.Args[0], "-o", tdir.DirPath, "rw.go.txt"})

	cv.Convey("Given bambam generated go bindings, \n"+
		"        then we should be able to write to disk, and read back the same structure", t, func() {

		cv.So(err, cv.ShouldEqual, nil)

		tdir.MoveTo()

		err = exec.Command("capnpc", "-ogo", "schema.capnp").Run()
		cv.So(err, cv.ShouldEqual, nil)

		err = exec.Command("go", "build").Run()
		cv.So(err, cv.ShouldEqual, nil)

		// run it
		err = exec.Command("./" + tdir.DirPath).Run()
		cv.So(err, cv.ShouldEqual, nil)

	})
}
Ejemplo n.º 6
0
func Test017WriteRead_StructPointerWithinStruct(t *testing.T) {

	tdir := NewTempDir()
	// comment the defer out to debug any rw test failures.
	defer tdir.Cleanup()

	err := exec.Command("cp", "rw3.go.txt", tdir.DirPath+"/rw3.go").Run()
	if err != nil {
		panic(err)
	}

	MainArgs([]string{os.Args[0], "-o", tdir.DirPath, "rw3.go.txt"})

	cv.Convey("Given bambam generated go bindings: with a struct pointer within a struct", t, func() {
		cv.Convey("then we should be able to write to disk, and read back the same structure", func() {
			cv.So(err, cv.ShouldEqual, nil)

			tdir.MoveTo()

			err = exec.Command("capnpc", "-ogo", "schema.capnp").Run()
			cv.So(err, cv.ShouldEqual, nil)

			err = exec.Command("go", "build").Run()
			cv.So(err, cv.ShouldEqual, nil)

			// run it
			err = exec.Command("./" + tdir.DirPath).Run()
			cv.So(err, cv.ShouldEqual, nil)

		})
	})
}
Ejemplo n.º 7
0
func TestCreationOfZData(t *testing.T) {
	const n = 20
	seg, _ := zdataFilledSegment(n)
	text := CapnpDecodeSegment(seg, "", "aircraftlib/aircraft.capnp", "Z")

	expectedText := `(zdata = (data = "\x00\x01\x02\x03\x04\x05\x06\a\b\t\n\v\f\r\x0e\x0f\x10\x11\x12\x13"))`

	cv.Convey("Given a go-capnproto created Zdata DATA element with n=20", t, func() {
		cv.Convey("When we decode it with capnp", func() {
			cv.Convey(fmt.Sprintf("Then we should get the expected text '%s'", expectedText), func() {
				cv.So(text, cv.ShouldEqual, expectedText)
			})
			cv.Convey("And our data should contain Z_ZDATA with contents 0,1,2,...,n", func() {
				z := air.ReadRootZ(seg)
				cv.So(z.Which(), cv.ShouldEqual, air.Z_ZDATA)

				var data []byte = z.Zdata().Data()
				cv.So(len(data), cv.ShouldEqual, n)
				for i := range data {
					cv.So(data[i], cv.ShouldEqual, i)
				}

			})
		})
	})

}
Ejemplo n.º 8
0
func TestWebGoesUpAndDown(t *testing.T) {

	// *** universal test cfg setup
	skipbye := false
	cfg := NewTestConfig() // bumps cfg.TestportBump so cfg.GetWebPort() is different in test.
	defer cfg.ByeTestConfig(&skipbye)
	// *** end universal test setup

	s := NewWebServer()
	cv.Convey("NewWebServer() should bring up a debug web-server", t, func() {
		cv.So(PortIsBound(s.Addr), cv.ShouldEqual, true)
		by, err := FetchUrl("http://" + s.Addr + "/debug/pprof")
		cv.So(err, cv.ShouldEqual, nil)
		//fmt.Printf("by:'%s'\n", string(by))
		cv.So(strings.HasPrefix(string(by), `<html>
<head>
<title>/debug/pprof/</title>
</head>
/debug/pprof/<br>
<br>`), cv.ShouldEqual, true)

	})

	cv.Convey("WebServer::Stop() should bring down the debug web-server", t, func() {
		s.Stop()
		cv.So(PortIsBound(s.Addr), cv.ShouldEqual, false)
	})
}
Ejemplo n.º 9
0
func Test002ListListStructList(t *testing.T) {

	cv.Convey("Given type RWTest struct { NestMatrix [][]Nester1; } in go, where Nester1 is a struct, and a mirror/parallel capnp struct air.RWTestCapn { nestMatrix @0: List(List(Nester1Capn)); } defined in the aircraftlib schema", t, func() {
		cv.Convey("When we Save() RWTest to capn and then Load() it back, the data should match, so that we have working List(List(Struct)) serialization and deserializatoin in go-capnproto", func() {

			// full RWTest
			rw := RWTest{
				NestMatrix: [][]Nester1{[]Nester1{Nester1{Strs: []string{"z", "w"}}, Nester1{Strs: []string{"q", "r"}}}, []Nester1{Nester1{Strs: []string{"zebra", "wally"}}, Nester1{Strs: []string{"qubert", "rocks"}}}},
			}

			var o bytes.Buffer
			rw.Save(&o)

			seg, n, err := capn.ReadFromMemoryZeroCopy(o.Bytes())
			cv.So(err, cv.ShouldEqual, nil)
			cv.So(n, cv.ShouldBeGreaterThan, 0)

			text := CapnpDecodeSegment(seg, "", "aircraftlib/aircraft.capnp", "RWTestCapn")

			if false {
				fmt.Printf("text = '%s'\n", text)
			}

			rw2 := &RWTest{}
			rw2.Load(&o)

			//fmt.Printf("rw = '%#v'\n", rw)
			//fmt.Printf("rw2 = '%#v'\n", rw2)

			same := reflect.DeepEqual(&rw, rw2)
			cv.So(same, cv.ShouldEqual, true)
		})
	})
}
Ejemplo n.º 10
0
func TestPrint(t *testing.T) {

	seg := capn.NewBuffer(nil)
	z := air.NewRootZ(seg)
	airc := air.AutoNewAircraft(seg)
	b737 := air.AutoNewB737(seg)
	base := air.AutoNewPlaneBase(seg)
	base.SetName("helen")
	base.SetMaxSpeed(0.5)
	homes := air.NewAirportList(seg, 2)
	homes.Set(0, air.AIRPORT_JFK)
	homes.Set(1, air.AIRPORT_SFO)
	base.SetHomes(homes)
	b737.SetBase(base)
	airc.SetB737(b737)
	z.SetAircraft(airc)
	lit, err := z.MarshalCapLit()
	panicOn(err)
	json, err := z.MarshalJSON()
	panicOn(err)

	cv.Convey("Given the aircraftlib schema (and an Aircraft value), we should generate a MarshalCapLit() function that returns a literal representation in bytes for the given Aircraft value. And the MarshalJSON() should return the expected format too.", t, func() {
		cv.So(string(lit), cv.ShouldEqual, `(aircraft = (b737 = (base = (name = "helen", homes = [jfk, sfo], rating = 0, canFly = false, capacity = 0, maxSpeed = 0.5))))`)
		cv.So(string(json), cv.ShouldEqual, `{"aircraft":{"b737":{"base":{"name":"helen","homes":["jfk", "sfo"],"rating":0,"canFly":false,"capacity":0,"maxSpeed":0.5}}}}`)
	})

}
Ejemplo n.º 11
0
// start with smaller Struct(List)
func Test001StructList(t *testing.T) {

	cv.Convey("Given type Nester1 struct { Strs []string } in go, where Nester1 is a struct, and a mirror/parallel capnp struct air.Nester1Capn { strs @0: List(Text); } defined in the aircraftlib schema", t, func() {
		cv.Convey("When we Save() Nester to capn and then Load() it back, the data should match, so that we have working Struct(List) serialization and deserializatoin in go-capnproto", func() {

			// Does Nester1 alone serialization and deser okay?
			rw := Nester1{Strs: []string{"xenophilia", "watchowski"}}

			var o bytes.Buffer
			rw.Save(&o)

			seg, n, err := capn.ReadFromMemoryZeroCopy(o.Bytes())
			cv.So(err, cv.ShouldEqual, nil)
			cv.So(n, cv.ShouldBeGreaterThan, 0)

			text := CapnpDecodeSegment(seg, "", "aircraftlib/aircraft.capnp", "Nester1Capn")
			if false {
				fmt.Printf("text = '%s'\n", text)
			}
			rw2 := &Nester1{}
			rw2.Load(&o)

			//fmt.Printf("rw = '%#v'\n", rw)
			//fmt.Printf("rw2 = '%#v'\n", rw2)

			same := reflect.DeepEqual(&rw, rw2)
			cv.So(same, cv.ShouldEqual, true)
		})
	})
}
Ejemplo n.º 12
0
func TestDecompressorVerbosely(t *testing.T) {
	cv.Convey("Testing the go-capnproto Decompressor.Read() function:", t, func() {
		for _, test := range compressionTests {

			fmt.Printf("\n\nGiven compressed text '%#v'\n", test.compressed)

			//	fmt.Printf("   test.original = %#v\n test.compressed = %#v\n", test.original, test.compressed)
			for readSize := 1; readSize <= 8+2*len(test.original); readSize++ {
				fmt.Printf("\n  When we use go-capnproto NewDecompressor, with readSize: %d\n    Then we should get the original text back.", readSize)

				r := bytes.NewReader(test.compressed)
				d := capn.NewDecompressor(r)
				buf := make([]byte, readSize)
				var actual []byte
				for {
					n, err := d.Read(buf)
					actual = append(actual, buf[:n]...)
					if err != nil {
						if err == io.EOF {
							break
						}
						t.Fatalf("Read: %v", err)
					}
				}

				cv.So(len(actual), cv.ShouldResemble, len(test.original))
				if len(test.original) > 0 {
					cv.So(actual, cv.ShouldResemble, test.original)
				}
			} // end readSize loop
		}
		fmt.Printf("\n\n")
	})
}
Ejemplo n.º 13
0
func TestDecodeOnKnownWellPackedData(t *testing.T) {

	// length 17
	byteSliceIn := []byte{0x10, 0x5, 0x50, 0x2, 0x1, 0x1, 0x25, 0x0, 0x0, 0x11, 0x1, 0xc, 0xf, 0xd4, 0x7, 0xc, 0x7}
	fmt.Printf("len of byteSliceIn is %d\n", len(byteSliceIn))
	// annotated: byteSliceIn := []byte{tag:0x10, 0x5, tag:0x50, 0x2, 0x1, tag:0x1, 0x25, tag:0x0, 0x0, tag:0x11, 0x1, 0xc, tag:0xf, 0xd4, 0x7, 0xc, 0x7}

	// length 48
	expectedOut := []byte{0x0, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x1, 0x0, 0x25, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0xd4, 0x7, 0xc, 0x7, 0x0, 0x0, 0x0, 0x0}

	r := bytes.NewReader(byteSliceIn)
	actual, err := ioutil.ReadAll(capn.NewDecompressor(r))
	if err != nil {
		panic(err)
	}

	cv.Convey("Given a known-to-be-correctly packed 17-byte long sequence for a ZdateVector holding a single Zdate", t, func() {
		cv.Convey("When we use go-capnproto NewDecompressor", func() {
			cv.Convey("Then we should get the same unpacked bytes as capnp provides", func() {
				cv.So(len(actual), cv.ShouldResemble, len(expectedOut))
				cv.So(actual, cv.ShouldResemble, expectedOut)
			})
		})
	})

}
Ejemplo n.º 14
0
func Test014TranslationOfArraysOfInterfacesEmbeddedWorks(t *testing.T) {

	cv.Convey(`Given a parent Snoopy struct that has an array of Flyer interfaces that are embedded inside Plane, these should be translated from Sexp correctly.`, t, func() {

		env := NewGlisp()
		env.StandardSetup()

		x, err := env.EvalString(`
(def he (hellcat speed:567))
(def ho (hornet SpanCm:12))
(def snoop (snoopy friends:[he ho]))
`)
		panicOn(err)
		cv.So(x.SexpString(), cv.ShouldEqual, ` (snoopy friends:[ (hellcat speed:567)  (hornet SpanCm:12)])`)

		var sn Snoopy
		_, err = SexpToGoStructs(x, &sn, env)
		panicOn(err)
		fmt.Printf("\n sn = %#v\n", sn)
		cv.So(&sn, cv.ShouldResemble, &Snoopy{
			Plane: Plane{
				Friends: []Flyer{
					&Hellcat{Plane: Plane{Speed: 567}},
					&Hornet{Plane: Plane{
						Wings: Wings{
							SpanCm: 12,
						},
					},
					},
				},
			},
		})

	})
}
Ejemplo n.º 15
0
func Test030LexingPauseAndResume(t *testing.T) {

	cv.Convey(`to enable the repl to properly detect the end of a multiline expression (or an expression containing quoted parentheses), the lexer should be able to pause and resume when more input is available.`, t, func() {

		str := `(defn hello [] "greetings!(((")`
		str1 := `(defn hel`
		str2 := `lo [] "greetings!(((")`
		env := NewGlisp()
		defer env.parser.Stop()
		stream := bytes.NewBuffer([]byte(str1))
		env.parser.ResetAddNewInput(stream)
		ex, err := env.parser.ParseTokens()

		P("\n In lexer_test, after parsing with incomplete input, we should get 0 expressions back.\n")
		cv.So(len(ex), cv.ShouldEqual, 0)
		P("\n In lexer_test, after ParseTokens on incomplete fragment, expressions = '%v' and err = '%v'\n", (&SexpArray{Val: ex, Env: env}).SexpString(nil), err)

		P("\n In lexer_test: calling parser.NewInput() to provide str2='%s'\n", str2)
		env.parser.NewInput(bytes.NewBuffer([]byte(str2)))
		P("\n In lexer_test: done with parser.NewInput(), now calling parser.ParseTokens()\n")
		ex, err = env.parser.ParseTokens()
		P(`
 in lexer test: After providing the 2nd half of the input, we returned from env.parser.ParseTokens()
 with expressions = %v
 with err = %v
`, (&SexpArray{Val: ex, Env: env}).SexpString(nil), err)

		cv.So(len(ex), cv.ShouldEqual, 1)
		panicOn(err)

		P("str=%s\n", str)
	})
}
Ejemplo n.º 16
0
func Test018ReflectCallOnGoMethodsComplexReturnType(t *testing.T) {

	cv.Convey(`Given a translated to Go structs, we should be able`+
		` to invoke methods on them that return struct pointers`,
		t, func() {

			env := NewGlisp()
			defer env.parser.Stop()

			env.StandardSetup()

			x, err := env.EvalString(`
(def he (hellcat speed:567))
(def ho (hornet SpanCm:12))
(def snoop (snoopy friends:[he ho] cry:"yowza"))
`)
			panicOn(err)
			cv.So(x.SexpString(), cv.ShouldEqual, ` (snoopy friends:`+
				`[ (hellcat speed:567)  (hornet SpanCm:12)] cry:"yowza")`)

			var sn Snoopy
			_, err = SexpToGoStructs(x, &sn, env)
			panicOn(err)
			VPrintf("\n sn = %#v\n", sn)

			invok, err := env.EvalString(`
			   (_method snoop EchoWeather: (weather time:(now) size:12 ` +
				`type:"sunny" details:(raw "123")))
			   `)
			panicOn(err)
			VPrintf("got invoke = '%s'\n", invok.SexpString())
			cv.So(invok.SexpString(), cv.ShouldEqual, `[ (weather time:()`+
				` size:12 type:"sunny" details:[]byte{0x31, 0x32, 0x33})]`)
		})
}
Ejemplo n.º 17
0
func Test010WriteIntoSingleInterfaceValueWorks(t *testing.T) {

	cv.Convey(`Given a parent Snoopy struct that has an interface scalar`+
		` value, this should translate from Sexp to Go correctly.`, t, func() {

		env := NewGlisp()
		defer env.parser.Stop()

		env.StandardSetup()

		x, err := env.EvalString(`
(def he (hellcat speed:567))
(def ho (hornet))
(def snoop (snoopy chld:he))
`)
		panicOn(err)

		cv.So(x.SexpString(), cv.ShouldEqual, ` (snoopy chld: (hellcat speed:567))`)

		var sn Snoopy
		_, err = SexpToGoStructs(x, &sn, env)
		panicOn(err)
		VPrintf("\n sn = %#v\n", sn)
		cv.So(sn.Chld, cv.ShouldResemble, &Hellcat{Plane: Plane{Speed: 567}})

	})
}
Ejemplo n.º 18
0
func Test007ParentChildRecordsTranslateToGo(t *testing.T) {

	cv.Convey(`Given a tree of three records (hashes in zygomys); a snoopy`+
		` containing a hellcat, then SexpToGoStructs() should translate`+
		` that parent-child relationship faithfully into a Go Snoopy{}`+
		` containing a Go Hellcat{}.`, t, func() {

		env := NewGlisp()
		defer env.parser.Stop()

		env.StandardSetup()

		x, err := env.EvalString(`
(def he (hellcat speed:567))
(def snoop (snoopy chld:he))
`)
		panicOn(err)

		cv.So(x.SexpString(), cv.ShouldEqual, ` (snoopy chld: (hellcat speed:567))`)

		var sn Snoopy
		_, err = SexpToGoStructs(x, &sn, env)
		panicOn(err)
		VPrintf("\n sn = %#v\n", sn)
		cv.So(sn.Chld, cv.ShouldResemble, &Hellcat{Plane: Plane{Speed: 567}})
	})
}
Ejemplo n.º 19
0
func Test009CallByReflectionWorksWithoutNestingWithoutEmbeds(t *testing.T) {

	cv.Convey(`Given an un-nested record without references to other`+
		` records; and without embedded structs; we should translate`+
		` from record to Go struct correctly`, t, func() {

		env := NewGlisp()
		defer env.parser.Stop()

		env.StandardSetup()

		x, err := env.EvalString(`
(def ho (hornet nickname:"Bob" mass:4.2))
`)
		panicOn(err)

		cv.So(x.SexpString(), cv.ShouldEqual,
			` (hornet nickname:"Bob" mass:4.2)`)

		ho := &Hornet{}
		res, err := SexpToGoStructs(x, ho, env)
		panicOn(err)
		VPrintf("\n ho = %#v\n", ho)
		VPrintf("\n res = %#v\n", res)
		cv.So(ho, cv.ShouldResemble, &Hornet{Nickname: "Bob", Mass: 4.2})
	})
}
Ejemplo n.º 20
0
func Test008CallByReflectionWorksWithoutNesting(t *testing.T) {

	cv.Convey(`Given an un-nested record without references to`+
		` other records; we should translate from record to Go`+
		` struct correctly`, t, func() {

		env := NewGlisp()
		defer env.parser.Stop()

		env.StandardSetup()

		x, err := env.EvalString(`
(def ho (hornet speed:567 nickname:"Bob" mass:4.2 SpanCm:8877))
`)
		panicOn(err)

		cv.So(x.SexpString(), cv.ShouldEqual,
			` (hornet speed:567 nickname:"Bob" mass:4.2 SpanCm:8877)`)

		ho := &Hornet{}
		res, err := SexpToGoStructs(x, ho, env)
		panicOn(err)
		VPrintf("\n ho = %#v\n", ho)
		VPrintf("\n res = %#v\n", res)
		cv.So(ho, cv.ShouldResemble, &Hornet{Plane: Plane{
			Wings: Wings{SpanCm: 8877}, Speed: 567},
			Nickname: "Bob", Mass: 4.2})
	})
}
Ejemplo n.º 21
0
func TestStripNanoAddr(t *testing.T) {

	cv.Convey("StripNanomsgAddressPrefix should omit the 'tcp://' stuff", t, func() {
		strip, err := StripNanomsgAddressPrefix("tcp://a.b.c.d:9090")
		cv.So(strip, cv.ShouldEqual, "a.b.c.d:9090")
		cv.So(err, cv.ShouldEqual, nil)
	})
}
Ejemplo n.º 22
0
func TestGeistRequiresArg1(t *testing.T) {
	Build()
	cv.Convey("Given the geist executable, when geist is invoked, it must require one argument (the path to the script it should execute) or fail complaining a path is required.", t, func() {
		_, errbuf, err := Run(".", "./geist")
		cv.So(err, cv.ShouldNotEqual, nil)
		cv.So(errbuf.String(), cv.ShouldEqual, "geist error: missing path to script to run as argument to geist.")
	})
}
Ejemplo n.º 23
0
func TestMarshal(t *testing.T) {

	cv.Convey("Given go struct Extra", t, func() {
		cv.Convey("then the generated ExtraCapntoGo() code should copy content from an ExtraCapn to an Extra struct.", func() {
			cv.Convey("and should handle int fields", func() {
				ex0 := `
type Extra struct {
  A int
  B int
}`
				toGoCode := ExtractCapnToGoCode(ex0, "Extra")
				cv.So(toGoCode, ShouldMatchModuloWhiteSpace, `
func ExtraCapnToGo(src ExtraCapn, dest *Extra) *Extra { 
  if dest == nil { 
     dest = &Extra{} 
  }
  dest.A = int(src.A())
  dest.B = int(src.B())
  return dest } 
`)

				toCapnCode := ExtractGoToCapnCode(ex0, "Extra")
				cv.So(toCapnCode, ShouldMatchModuloWhiteSpace, `
func ExtraGoToCapn(seg *capn.Segment, src *Extra) ExtraCapn { 
  dest := AutoNewExtraCapn(seg)
  dest.SetA(int64(src.A))
  dest.SetB(int64(src.B))
  return dest } 
`)

			})
			cv.Convey("and should handle uint fields", func() {
				ex0 := `
type Extra struct {
  A uint64
}`
				toGoCode := ExtractCapnToGoCode(ex0, "Extra")
				cv.So(toGoCode, ShouldMatchModuloWhiteSpace, `
func ExtraCapnToGo(src ExtraCapn, dest *Extra) *Extra {
  if dest == nil {
     dest = &Extra{}
  }
  dest.A = src.A()
  return dest }
`)

				toCapnCode := ExtractGoToCapnCode(ex0, "Extra")
				cv.So(toCapnCode, ShouldMatchModuloWhiteSpace, `
func ExtraGoToCapn(seg *capn.Segment, src *Extra) ExtraCapn {
  dest := AutoNewExtraCapn(seg)
  dest.SetA(src.A)
  return dest }
`)
			})
		})
	})
}
Ejemplo n.º 24
0
func Test028FloatingPointRegex(t *testing.T) {

	cv.Convey("our lexer should recognize negative floating point and negative integers", t, func() {
		ans := DecimalRegex.MatchString(`-1`)
		cv.So(ans, cv.ShouldEqual, true)
		ans = FloatRegex.MatchString(`-1e-10`)
		cv.So(ans, cv.ShouldEqual, true)
	})
}
Ejemplo n.º 25
0
func Test030DedupStreams(t *testing.T) {
	cv.Convey(`given a stream with some duplicate frames repeating previously seen frames, Dedup() filter out duplicates within the given window`, t, func() {

		// setup the test streams, with duplicated Frames
		nFrame := 100
		expectedPath := "test.dedup.expected"

		frames, _, _ := GenTestFrames(nFrame, &expectedPath)

		// prep to write out duplicates
		dupsPath := "test.dups"
		writeMe, err := os.Create(dupsPath)
		panicOn(err)
		ds := NewFrameWriter(writeMe, 64*1024)

		// generate duplicates, randomly
		fold := 4
		tot := nFrame * fold
		k := 0

		// first be sure we've seen all our frames, so we
		// aren't jumping completely backwards before the first frame.
		for i := 0; i < nFrame; i++ {
			ds.Append(frames[i])
			k++
		}

		for i := 1; i <= tot; i++ {
			fr := frames[cryptoRandNonNegInt()%((i%(nFrame-1))+1)]
			ds.Append(fr)
			k++
		}
		ds.Flush()
		ds.Sync()
		writeMe.Seek(0, 0)
		hasDupsFd := writeMe

		obsPath := "test.dedup.obs"
		dedupFd, err := os.Create(obsPath)

		// dedup the stream
		// all of it, so we give the full total as the window
		p("k = %v", k)
		err = Dedup(hasDupsFd, dedupFd, k+1, nil, false)
		cv.So(err, cv.ShouldEqual, nil)

		// check
		diff := FilesDiff(expectedPath, obsPath)
		if diff {
			panic(fmt.Errorf("dedup failed, '%s' != '%s'", obsPath, expectedPath))
		}
		cv.So(diff, cv.ShouldBeFalse)
		os.Remove(obsPath)
		os.Remove(expectedPath)
	})
}
Ejemplo n.º 26
0
func Test040MakeFloat64Frames(t *testing.T) {
	cv.Convey("MakeFloat64Frames should generate a sequence of frames of equal spacing, yet omit frames with 0 specified in the delta sequence, to allow us to generate test sequences with gaps", t, func() {

		tm, err := time.Parse(time.RFC3339, "2016-03-10T00:00:00Z")
		panicOn(err)
		spacing := time.Second

		wa := int64(4)
		pa := int64(1)

		seq := []float64{-1, -5, -10, 0, -5, 15, 0, -2}
		typ := []int64{wa, wa, wa, 0, wa, pa, 0, wa}
		sec := []int64{0, 1, 2, 3, 4, 4, 5, 6}

		frames := MakeTwo64Frames(tm, seq, typ, sec)
		//		for i := range frames {
		//			p("frames[%v] = %s", i, *(frames[i]))
		//		}
		cv.So(len(frames), cv.ShouldEqual, 6)
		cv.So(frames[0].TmTime(), cv.ShouldResemble, tm)
		cv.So(frames[0].GetV0(), cv.ShouldResemble, float64(-1))
		cv.So(frames[1].GetV0(), cv.ShouldResemble, float64(-5))
		cv.So(frames[2].GetV0(), cv.ShouldResemble, float64(-10))
		cv.So(frames[3].GetV0(), cv.ShouldResemble, float64(-5))
		cv.So(frames[4].GetV0(), cv.ShouldResemble, float64(15))
		cv.So(frames[5].GetV0(), cv.ShouldResemble, float64(-2))
		cv.So(frames[5].TmTime(), cv.ShouldResemble, tm.Add(6*spacing))
	})
}
Ejemplo n.º 27
0
func TestSubmitRemote(t *testing.T) {

	cv.Convey("remotely, over nanomsg, goq should be able to submit job to the server", t, func() {
		cv.Convey("and then server should get back the expected output from the job run", func() {

			// allow all child processes to communicate

			// *** universal test cfg setup
			skipbye := false
			cfg := NewTestConfig()
			defer cfg.ByeTestConfig(&skipbye)
			// *** end universal test setup

			// ensure any previous test has released our port before proceeding.
			WaitUntilAddrAvailable(cfg.JservAddr())

			cfg.DebugMode = true
			childpid, err := NewExternalJobServ(cfg)
			if err != nil {
				panic(err)
			}
			fmt.Printf("[pid %d] spawned a new external JobServ with pid %d\n", os.Getpid(), childpid)

			j := NewJob()
			j.Cmd = "bin/good.sh"

			sub, err := NewSubmitter(GenAddress(), cfg, false)
			if err != nil {
				panic(err)
			}
			sub.SubmitJob(j)

			worker, err := NewWorker(GenAddress(), cfg, nil)
			if err != nil {
				panic(err)
			}
			jobout, err := worker.DoOneJob()
			if err != nil {
				panic(err)
			}
			worker.Destroy()

			// *important* cleanup, and wait for cleanup to finish, so the next test can run.
			// has no Fromaddr, so crashes: SendShutdown(cfg.JservAddr, cfg)
			sub.SubmitShutdownJob()

			WaitForShutdownWithTimeout(childpid, cfg)

			cv.So(len(jobout.Out), cv.ShouldEqual, 2)
			cv.So(jobout.Out[0], cv.ShouldEqual, "I'm starting some work")
			cv.So(jobout.Out[1], cv.ShouldEqual, "I'm done with my work")

		})
	})
}
Ejemplo n.º 28
0
func Test026RegexpSplittingOfDotSymbols(t *testing.T) {

	cv.Convey("our DotPartsRegex should split dot-symbol `.a.b.c` into `.a`, `.b`, and `.c`", t, func() {
		target := ".a.b.c"
		path := DotPartsRegex.FindAllString(target, -1)
		fmt.Printf("path = %#v\n", path)
		cv.So(len(path), cv.ShouldEqual, 3)
		cv.So(path[0], cv.ShouldEqual, ".a")
		cv.So(path[1], cv.ShouldEqual, ".b")
		cv.So(path[2], cv.ShouldEqual, ".c")
	})
}
Ejemplo n.º 29
0
func TestTagAnnotationWorks(t *testing.T) {

	cv.Convey("Given a golang struct with an invalid capnp field name 'Union', but with a field tag: `capname:\"MyNewFieldName\"`", t, func() {
		cv.Convey("then we should use the capname MyNewFieldName for the field, and not stop the run.", func() {
			ex0 := "type S struct { Union string `capname:\"MyNewFieldName\"` \n}"
			cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `struct SCapn { MyNewFieldName  @0:   Text; } `)
			ex1 := "type S struct { Union string `capname: \t \t \"MyNewFieldName\"` \n}"
			cv.So(ExtractString2String(ex1), ShouldStartWithModuloWhiteSpace, `struct SCapn { MyNewFieldName  @0:   Text; } `)
		})

	})
}
Ejemplo n.º 30
0
func TestEnumFromString(t *testing.T) {
	cv.Convey("Given an enum tag string matching a constant", t, func() {
		cv.Convey("FromString should return the corresponding matching constant value", func() {
			cv.So(air.AirportFromString("jfk"), cv.ShouldEqual, air.AIRPORT_JFK)
		})
	})
	cv.Convey("Given an enum tag string that does not match a constant", t, func() {
		cv.Convey("FromString should return 0", func() {
			cv.So(air.AirportFromString("notEverMatching"), cv.ShouldEqual, 0)
		})
	})
}