Example #1
0
func TestRabaseShortRadix(t *testing.T) {
	convey.Convey("test method 'RabaseShortRadix', functions should be right after rebase", t, func() {
		err, idWorker := NewIdWorker(1)
		if err != nil {
			t.Fatal("can not initialize")
		}
		err, baseN := baseN4go.NewBaseN(int8(62))
		convey.So(err, convey.ShouldBeNil)
		convey.So(baseN, convey.ShouldNotBeNil)

		err, newId := idWorker.NextId()
		convey.So(err, convey.ShouldBeNil)
		convey.So(newId, convey.ShouldNotBeNil)
		err, shortId := idWorker.ShortenId(newId)
		convey.So(err, convey.ShouldBeNil)

		_, baseNId := baseN.Encode(newId)
		convey.So(shortId, convey.ShouldEqual, baseNId)

		idWorker.RabaseShortRadix(int8(16))
		err, newId = idWorker.NextId()
		err, shortId = idWorker.ShortenId(newId)

		err, baseN = baseN4go.NewBaseN(int8(16))
		_, baseNId = baseN.Encode(newId)
		convey.So(shortId, convey.ShouldEqual, baseNId)
	})
}
Example #2
0
//重置用于shorten id的基数,参见baseN4go实现
func (id *IdWorker) RabaseShortRadix(radix int8) error {
	err, baseN := baseN4go.NewBaseN(radix)
	if err != nil {
		return err
	}
	id.baseN4go = baseN
	return nil
}
Example #3
0
func TestShortId(t *testing.T) {
	convey.Convey("method 'ShortId' should return the right result", t, func() {
		err, idWorker := NewIdWorker(1)
		if err != nil {
			t.Fatal("can not initialize")
		}

		err, newIdStr := idWorker.ShortId()
		convey.So(err, convey.ShouldBeNil)
		convey.So(newIdStr, convey.ShouldHaveSameTypeAs, "abc")

		err, baseN := baseN4go.NewBaseN(int8(62))
		_, newId := baseN.Decode(newIdStr)
		convey.So(newId, convey.ShouldHaveSameTypeAs, int64(1))
	})
}
Example #4
0
func NewIdWorker(workerId int64) (error, *IdWorker) {
	idWorker := &IdWorker{}
	if workerId > maxWorkerId || workerId < 0 {
		return errors.New(fmt.Sprintf("illegal worker id: %d", workerId)), nil
	}

	idWorker.workerId = workerId
	idWorker.lastTimestamp = -1
	idWorker.sequence = 0
	idWorker.mutex = &sync.Mutex{}
	err, baseN := baseN4go.NewBaseN(int8(62)) //默认radix为62,详见baseN4go
	if err != nil {
		return errors.New("can not initialize 'baseN4go'"), nil
	}
	idWorker.baseN4go = baseN
	return nil, idWorker
}
Example #5
0
func TestShortenId(t *testing.T) {
	convey.Convey("method 'ShortenId' should return the right shorten id", t, func() {
		err, idWorker := NewIdWorker(1)
		if err != nil {
			t.Fatal("can not initialize")
		}
		err, baseN := baseN4go.NewBaseN(int8(62))
		convey.So(err, convey.ShouldBeNil)
		convey.So(baseN, convey.ShouldNotBeNil)

		err, newId := idWorker.NextId()
		convey.So(err, convey.ShouldBeNil)
		convey.So(newId, convey.ShouldNotBeNil)
		err, shortId := idWorker.ShortenId(newId)
		convey.So(err, convey.ShouldBeNil)

		_, baseNId := baseN.Encode(newId)
		convey.So(shortId, convey.ShouldEqual, baseNId)
	})
}