Example #1
0
// Input has to return the input channel for the
// date to process.
func (o *OrderMapReducer) Input() mapreduce.KeyValueChan {
	input := make(mapreduce.KeyValueChan)
	// Generate test orders and push them into a the
	// input channel.
	articleMaxNo := 9999
	unitPrices := make([]float64, articleMaxNo+1)
	for i := 0; i < articleMaxNo+1; i++ {
		unitPrices[i] = rand.Float64() * 100.0
	}
	go func() {
		defer close(input)
		for i := 0; i < o.count; i++ {
			order := &Order{
				OrderNo:    identifier.NewUUID(),
				CustomerNo: rand.Intn(999) + 1,
				Items:      make([]*OrderItem, rand.Intn(9)+1),
			}
			for j := 0; j < len(order.Items); j++ {
				articleNo := rand.Intn(articleMaxNo)
				order.Items[j] = &OrderItem{
					ArticleNo:    articleNo,
					Count:        rand.Intn(9) + 1,
					UnitPrice:    unitPrices[articleNo],
					DiscountPerc: rand.Float64() * 4.0,
				}
				o.items[articleNo] = append(o.items[articleNo], order.Items[j])
			}
			input <- order
		}
	}()
	return input
}
Example #2
0
func wsRoomHandler(w http.ResponseWriter, r *http.Request) {
	user := r.URL.Query().Get("user")

	if user == "" || user == "undefined" {
		http.Error(w, "user name not specified", http.StatusTeapot)
		return
	}

	vars := mux.Vars(r)
	building := vars["building"]
	room := vars["room"]
	roomID := makeRoomID(room, building)

	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Fatal(err)
	}

	wsb := newWebSocketBehavior(ws, user)
	id := identifier.Identifier("wsb", user, identifier.NewUUID())
	rootEnvironment.StartCell(id, wsb)
	subscribe(rootEnvironment, roomID, id)

	userID := addUser(rootEnvironment, building, room, user)

	defer func() {
		wsb.sendUserLeaveEvent()
		rootEnvironment.StopCell(userID)
		rootEnvironment.StopCell(id)
	}()

	go wsb.writePump()
	wsb.readPump()
}
Example #3
0
// Test the creation of identifiers based on types.
func TestTypeAsIdentifierPart(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	// Type as identifier.
	var tai TypeToSplitForIdentifier

	id := identifier.TypeAsIdentifierPart(tai)
	assert.Equal(id, "type-to-split-for-identifier", "wrong TypeAsIdentifierPart() result")

	id = identifier.TypeAsIdentifierPart(identifier.NewUUID())
	assert.Equal(id, "u-u-i-d", "wrong TypeAsIdentifierPart() result")
}
Example #4
0
// StartLimited creates and runs a new scene with an inactivity
// and an absolute timeout. They may be zero.
func StartLimited(inactivity, absolute time.Duration) Scene {
	s := &scene{
		id:          identifier.NewUUID(),
		props:       make(map[string]*box),
		flags:       make(map[string]bool),
		signalings:  make(map[string][]chan struct{}),
		inactivity:  inactivity,
		absolute:    absolute,
		commandChan: make(chan *envelope, 1),
	}
	s.backend = loop.Go(s.backendLoop)
	return s
}
Example #5
0
// NewEnvironment creates a new environment.
func NewEnvironment(idParts ...interface{}) Environment {
	var id string
	if len(idParts) == 0 {
		id = identifier.NewUUID().String()
	} else {
		id = identifier.Identifier(idParts...)
	}
	env := &environment{
		id:    id,
		cells: newRegistry(),
	}
	runtime.SetFinalizer(env, (*environment).Stop)
	logger.Infof("cells environment %q started", env.ID())
	return env
}
Example #6
0
// Test the standard UUID.
func TestStandardUUID(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	// Asserts.
	uuid := identifier.NewUUID()
	assert.Equal(uuid.Version(), identifier.UUIDv4)
	uuidStr := uuid.String()
	assert.Equal(len(uuid), 16, "UUID length has to be 16")
	assert.Match(uuidStr, "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", "UUID has to match")
	// Check for unique creation, but only weak test.
	uuids := make(map[string]bool)
	for i := 0; i < 1000000; i++ {
		uuid = identifier.NewUUID()
		uuidStr = uuid.String()
		assert.False(uuids[uuidStr], "UUID collision must not happen")
		uuids[uuidStr] = true
	}
	// Check for copy.
	uuidA := identifier.NewUUID()
	uuidB := uuidA.Copy()
	for i := 0; i < len(uuidA); i++ {
		uuidA[i] = 0
	}
	assert.Different(uuidA, uuidB)
}