Пример #1
0
func TestDb2(t *testing.T) {
	fmt.Println("Test 2b - Insert Data (Parallel)")
	numCount := pnumCount
	workerCount := pworkerCount
	idxs := make([]interface{}, numCount)
	for i := 1; i <= numCount; i++ {
		idxs[i-1] = i + 2000
	}

	conn := mongodb.NewConnection("localhost:27888", "root", "Database.1", "ectest")
	//conn := mongodb.NewConnection("localhost:27888", "", "", "ectest")
	e = conn.Connect()
	if e != nil {
		t.Error("Unable to connect to Database | " + e.Error())
		return
	}
	defer conn.Close()
	_, e = conn.Execute("appusers", toolkit.M{"find": toolkit.M{}, "operation": base.DB_DELETE})
	if e != nil {
		fmt.Println("Unable to delete: " + e.Error())
	}

	fmt.Printf("Insert %d data within %d pools \n", numCount, workerCount)
	pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
		for jb := range js {
			j := jb.(int)
			user := toolkit.M{}
			userid := "User " + strconv.Itoa(j)
			user.Set("_id", userid)
			user.Set("fullname", "User "+strconv.Itoa(j))
			user.Set("email", "user"+strconv.Itoa(j)+"@email.com")
			r := new(toolkit.Result)
			//_, _, e = conn.Query().From("ORMUsers").Save(user).Run()
			_, e := conn.Execute("appusers",
				toolkit.M{"find": toolkit.M{"_id": userid}, "operation": base.DB_SAVE, "data": user})
			if e == nil {
				r.Status = toolkit.Status_OK
				r.Data = user
			} else {
				r.Status = toolkit.Status_NOK
				r.Message = "Error ID " + strconv.Itoa(j) + " " + e.Error()
			}
			rs <- r
		}
	})

	if pr.Success == numCount {
		fmt.Printf("Test OK in %v \n\n", pr.Duration)
	} else {
		fmt.Printf("Test Fail has %d records. \nErrors\n%v\nIn %v \n\n", pr.Success, toolkit.JsonString(pr.Errors[0]), pr.Duration)
	}
}
Пример #2
0
func Test1(t *testing.T) {
	t.Skip()
	fmt.Println("Test 1 - Generate x Number")
	numCount := 1000
	workerCount := 100
	idxs := make([]interface{}, numCount)
	want := 0
	for i := 1; i <= numCount; i++ {
		idxs[i-1] = i
		want += i
	}

	total := 0
	fmt.Printf("Run Parallel %d job within %d pools \n", numCount, workerCount)
	pr := Run(idxs, workerCount, func(js <-chan interface{}, rs chan<- *toolkit.Result) {
		for j := range js {
			time.Sleep(1 * time.Microsecond)
			j2 := j.(int)
			total += j2
			r := new(toolkit.Result)
			r.Status = toolkit.Status_OK
			r.Data = j2
			rs <- r
		}
	})

	if total == want && pr.Success == 1000 {
		fmt.Printf("Test OK in %v \n\n", pr.Duration)
	} else {
		fmt.Printf("Test Fail want %d  got %d and has %d records\n", total, want, pr.Success)
	}
}
Пример #3
0
func (s *Subscriber) Start(address string) error {
	s.Address = address
	s.Actions = map[string]FnTrigger{}
	s.MessageQues = map[string]*MessageQue{}
	broadcasterUrl := s.BroadcasterAddress + "/nodeadd?node=" + s.Address
	r, e := toolkit.HttpCall(broadcasterUrl, "GET", nil, nil)
	if e != nil {
		return fmt.Errorf("Unable to contact Broadcaster Server. %s", e.Error())
	}
	if sOk := toolkit.HttpContentString(r); sOk != "OK" {
		return fmt.Errorf("Unable to add %s as subsriber to %s. Error: %s", s.Address, s.BroadcasterAddress, sOk)
	}

	//-- inform subscriber if newkey is available to be collected
	s.Route("/newkey", func(k *knot.WebContext) interface{} {
		result := toolkit.NewResult()
		k.Config.OutputType = knot.OutputJson
		msg := new(MessageQue)
		e := k.GetPayload(&msg)
		if e != nil {
			result.Message = e.Error()
			result.Status = toolkit.Status_NOK
		} else {
			msg.Data = nil
			msg.Collected = false
			_, exist := s.MessageQues[msg.Key]
			if !exist {
				s.messageKeys = append(s.messageKeys, msg.Key)
			}
			s.MessageQues[msg.Key] = msg
			//s.messageKeys = append(s.messageKeys, msg.Key)
		}
		return result
	})

	s.Route("/getmsg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		key := k.Query("key")
		result := s.getMsgAsResult(key)
		return result
	})

	//-- replicate message to subscribe
	s.Route("/msg", func(k *knot.WebContext) interface{} {
		k.Config.OutputType = knot.OutputJson
		result := toolkit.NewResult()
		var payload Message
		eDecode := k.GetPayload(&payload)
		if eDecode != nil {
			result.Status = toolkit.Status_NOK
			result.Message = fmt.Sprintf("Subscriber Message Decode Error %s", eDecode.Error())
		} else {
			result.Data = payload
		}
		return result
	})

	//-- stop the server
	s.Route("/stop", func(k *knot.WebContext) interface{} {
		defer k.Server.Stop()
		k.Config.OutputType = knot.OutputJson
		result := new(toolkit.Result)
		result.Status = "OK"
		return result
	})

	go func() {
		s.Server.Listen()
	}()
	return nil
}