示例#1
0
func TestFullWriteRead(t *testing.T) {
	const num = 1000
	const maxmax = 1e6
	const size = 1e6
	bufs := make([][]byte, num)
	numGoStart := runtime.NumGoroutine()
	buf := NewBuffer()
	defer func() {
		buf.Close()
		numGoStop := runtime.NumGoroutine()
		t.Log("NumGoroutine:", numGoStart, numGoStop, numGoStop-numGoStart)
	}()
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(size, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		in, err := rand.Bytes(int(n)+1, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		bufs[i] = in
		_, err = buf.Write(in)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(maxmax, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		max := int(n) + 1

		in := bufs[i]
		size := len(in)
		if size > max {
			size = max
		}
		b := make([]byte, len(in))

		for count := 0; count < len(in); {
			end := len(b)
			if count+size < end {
				end = count + size
			}
			n, err := buf.Read(b[count:end])
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			count += n
		}
		if !bytes.Equal(in, b) {
			t.Fatal("data not equal")
		}
	}
}
示例#2
0
func TestStream(t *testing.T) {
	//ulimit -n 1000
	//defer leaktest.Check(t)()

	var wg sync.WaitGroup

	t.Log("Num:", numClientTest)

	wg.Add(numClientTest)

	s1 := &Struct1Client{
		Net:         "tcp",
		Addrs:       server.Address().String(),
		Auth:        id,
		Session:     "1",
		Instance:    "2",
		PointerRecv: true,
	}

	err := s1.Init(true)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	var lck sync.Mutex

	for i := 0; i < numClientTest; i++ {
		go func(i int) {
			defer wg.Done()
			n, err := rand.Int63n(100, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			//println("Waiting...", i)
			time.Sleep(time.Duration(n) * time.Millisecond)
			//println("Running...", i)
			n, err = rand.Int63n(500, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			s1.Wait(time.Duration(n) * time.Millisecond)
			lck.Lock()
			err = s1.Error()
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			lck.Unlock()
			//println("End...", i)
		}(i)
	}
	wg.Wait()
	//println("Close...")
	err = s1.CloseClient()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#3
0
func TestProtoWriteReadPartial(t *testing.T) {
	const num = 10
	const maxmax = 1e2
	const size = 1e6
	bufs := make([][]byte, num)
	fifo := NewNotAFifo(100)
	defer fifo.Close()
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(size, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		in, err := rand.Bytes(int(n)+1, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		bufs[i] = in
		err = fifo.Write(in)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(maxmax, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		max := int(n) + 1

		in := bufs[i]
		size := len(in)
		if size > max {
			size = max
		}
		buf := make([]byte, len(in))

		for count := 0; count < len(in); {
			end := len(buf)
			if count+size < end {
				end = count + size
			}
			n, err := fifo.Read(buf[count:end])
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}

			count += n
		}
		if !bytes.Equal(in, buf) {
			t.Fatal("data not equal")
		}
	}
}
示例#4
0
func TestConnections(t *testing.T) {
	//defer leaktest.Check(t)()

	var wg sync.WaitGroup

	wg.Add(numClientTest)

	for i := 0; i < numClientTest; i++ {
		go func(i int) {
			defer wg.Done()
			n, err := rand.Int63n(10, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			//println("Configuring...", i)
			time.Sleep(time.Duration(n) * time.Millisecond)
			s1 := &Struct1Client{
				Net:         "tcp",
				Addrs:       server.Address().String(),
				Auth:        id,
				Session:     "1",
				Instance:    "2",
				PointerRecv: true,
			}
			//println("Connecting...", i)
			err = s1.Init(true)
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			//println("Running...", i)
			n, err = rand.Int63n(5000, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			s1.Wait(time.Duration(n) * time.Millisecond)
			err = s1.Error()
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			//println("Close...", i)
			err = s1.CloseClient()
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			//println("End...", i)
		}(i)
	}
	wg.Wait()
}
示例#5
0
func TestBigPriority(t *testing.T) {
	var numQueues uint64 = 1<<64 - 1
	q := NewPriority(numQueues, func() (Queue, error) {
		q := NewSimple(10, 1)
		return q, nil
	})
	defer q.Stop()
	q.Process()
	job := job.NewSimple("name", numQueues-1, func() error {
		delta, err := rand.Int63n(1000, "go")
		if err != nil {
			return e.Forward(err)
		}
		delta++
		time.Sleep(time.Duration(delta) * time.Millisecond)
		return nil
	})
	err := q.Add(job)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
	job.Returns(&err)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}
}
示例#6
0
func TestPriority(t *testing.T) {
	q := NewPriority(10, func(size int) (Queue, error) {
		q := NewSimple(size, 2)
		return q, nil
	}, 5)
	defer q.Stop()
	q.Process()

	jobs := make([]job.Job, NumJobsPrio)
	result := make([]string, NumJobsPrio)
	for i := 0; i < len(jobs); i++ {
		name := "test_" + strconv.FormatInt(int64(i), 10)
		prio, err := rand.Int63n(10, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		job := job.NewSimple(name, uint64(prio), func(i int) error {
			delta, err := rand.Int63n(1000, "go")
			if err != nil {
				return e.Forward(err)
			}
			delta++
			time.Sleep(time.Duration(delta) * time.Millisecond)
			result[i] = name
			return nil
		}, i)
		jobs[i] = job
		err = q.Add(job)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}

	for i, j := range jobs {
		var err error
		j.Returns(&err)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		name := "test_" + strconv.FormatInt(int64(i), 10)
		if result[i] != name {
			t.Fatal("result failed")
		}
	}
}
示例#7
0
func TestPrioritySequence(t *testing.T) {
	const num = 5
	q := NewPriority(num, func() (Queue, error) {
		q := NewSimple(10, 1)
		return q, nil
	})
	defer q.Stop()
	q.Process()

	datas := make([]time.Time, num)
	jobs := make([]job.Job, num)

	for i := 0; i < num; i++ {
		name := "test_" + strconv.FormatInt(int64(i), 10)
		job := job.NewSimple(name, uint64(i), func(i int) error {
			datas[i] = time.Now()
			delta, err := rand.Int63n(1000, "go")
			if err != nil {
				return e.Forward(err)
			}
			delta++
			time.Sleep(time.Duration(delta) * time.Millisecond)
			return nil
		}, i)
		err := q.Add(job)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		jobs[i] = job
	}

	for _, j := range jobs {
		var err error
		j.Returns(&err)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}

	for i := 0; i < len(datas)-1; i++ {
		if datas[i].After(datas[i+1]) {
			t.Fatal("not in sequence")
		}
	}
}
示例#8
0
func TestSimpleLen(t *testing.T) {
	q := NewSimple(10, 1)

	for i := 0; i < NumJobsPrio; i++ {
		name := "test_" + strconv.FormatInt(int64(i), 10)
		prio, err := rand.Int63n(10, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		job := job.NewSimple(name, uint64(prio), func(i int) int {
			return i
		}, i)
		err = q.Add(job)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}
	if q.Len() != NumJobsPrio {
		t.Fatal("Len failed:", q.Len())
	}
}
示例#9
0
func TestProtoWriteRead(t *testing.T) {
	const num = 10
	const size = 1e7
	bufs := make([][]byte, num)
	fifo := NewNotAFifo(100)
	defer fifo.Close()
	for i := 0; i < num; i++ {
		n, err := rand.Int63n(size, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		in, err := rand.Bytes(int(n)+1, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		bufs[i] = in
		err = fifo.Write(in)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
	}
	for i := 0; i < num; i++ {
		in := bufs[i]
		out := make([]byte, len(in))
		n, err := fifo.Read(out)
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		if n != len(in) {
			t.Fatal("don't read all data")
		}
		if !bytes.Equal(in, out) {
			t.Fatal("data not equal")
		}
	}
}
示例#10
0
func TestBarrierReaderParcialWriter(t *testing.T) {
	const size int = 1e6
	const wparcial int = 1e3
	const rparcial int = 1e3
	buf := NewBuffer()
	defer buf.Close()
	var wg sync.WaitGroup
	wg.Add(1)

	b, err := rand.Bytes(size, "go")
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	signal := make(chan struct{})

	go func() {
		defer wg.Done()
		b2 := make([]byte, len(b))
		count := 0
		signal <- struct{}{}
		for count < len(b) {
			l := wparcial + count
			if l > len(b2) {
				l = len(b2)
			}
			n, err := buf.Read(b2[count:l])
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			count += n
		}
		if count != len(b) {
			t.Fatal("length invalid")
		}
		if !bytes.Equal(b, b2) {
			t.Fatal("not equal")
		}
	}()

	<-signal

	bb := make([]byte, len(b))
	copy(bb, b)
	for len(bb) > 0 {
		d, err := rand.Int63n(10, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		time.Sleep(time.Duration(d) * time.Millisecond)
		l := rparcial
		if rparcial > len(bb) {
			l = len(bb)
		}
		n, err := buf.Write(bb[:l])
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		if n != l {
			t.Fatal("length invalid")
		}
		bb = bb[l:]
	}
	wg.Wait()
}
示例#11
0
func TestProtoWriteReadRandom(t *testing.T) {
	const num = 500
	const bufsize = 1e6
	var wg sync.WaitGroup
	wg.Add(num * 2)
	bufs := make([][]byte, num)
	fifo := NewNotAFifo(100)
	defer fifo.Close()
	for i := 0; i < num; i++ {
		// n, err := rand.Int63n(bufsize, "go")
		// if err != nil {
		// 	t.Fatal(e.Trace(e.Forward(err)))
		// }
		in, err := rand.Bytes(bufsize, "go")
		if err != nil {
			t.Fatal(e.Trace(e.Forward(err)))
		}
		bufs[i] = append(make([]byte, binary.MaxVarintLen32), in...)
		binary.PutVarint(bufs[i][0:binary.MaxVarintLen32], int64(i))
	}
	go func() {
		for i := 0; i < num; i++ {
			go func(i int) {
				defer wg.Done()
				n, err := rand.Int63n(100, "go")
				if err != nil {
					t.Fatal(e.Trace(e.Forward(err)))
				}
				time.Sleep(time.Duration(n) * time.Millisecond)
				//println("Start writer", i)
				err = fifo.Write(bufs[i])
				if err != nil {
					t.Fatal(e.Trace(e.Forward(err)))
				}
				//println("End writer", i)
			}(i)
		}
	}()
	go func() {
		for i := 0; i < num; i++ {
			go func(i int) {
				defer wg.Done()
				dur, err := rand.Int63n(100, "go")
				if err != nil {
					t.Fatal(e.Trace(e.Forward(err)))
				}
				time.Sleep(time.Duration(dur) * time.Millisecond)
				out := make([]byte, bufsize+binary.MaxVarintLen32)
				//println("Start reader", i)
				n, err := fifo.Read(out)
				if err != nil {
					t.Fatal(e.Trace(e.Forward(err)))
				}
				//println("End reader", i)
				n -= binary.MaxVarintLen32
				idx, b := binary.Varint(out[:binary.MaxVarintLen32])
				if b <= 0 {
					t.Fatal("can't decode the index")
				}
				if idx < 0 || idx > int64(len(bufs)) {
					t.Fatal("wrong idx", idx)
				}
				//println("Reader idx", idx)
				if n != bufsize {
					t.Fatal("don't read all data", n)
				}
				if !bytes.Equal(bufs[idx], out) {
					t.Fatal("data not equal")
				}
				//println("Stop reader", i)
			}(i)
		}
	}()
	wg.Wait()
}
示例#12
0
func TestLoad(t *testing.T) {
	//defer leaktest.Check(t)()

	//ulimit -n 1000
	var wg sync.WaitGroup
	const num = 5

	t.Log("Num:", num)

	wg.Add(num)

	err := instances.New("1", "3", reflect.ValueOf(&Load{}), owners)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	l := &LoadClient{
		Net:          "tcp",
		Addrs:        server.Address().String(),
		Auth:         id,
		Session:      "1",
		Instance:     "3",
		PointerRecv:  true,
		NumClones:    5,
		MaxNumClones: 10,
	}

	err = l.Init(true)
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

	for i := 0; i < num; i++ {
		go func(i int) {
			defer wg.Done()
			println("Starting", i)
			n, err := rand.Int63n(100, "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			time.Sleep(time.Duration(n) * time.Millisecond)
			// n, err = rand.Int63n(1e6, "go")
			// if err != nil {
			// 	t.Fatal(e.Trace(e.Forward(err)))
			// }
			n = 1e6
			in, err := rand.Bytes(int(n), "go")
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			out := l.Load(in)
			err = l.Error()
			if err != nil {
				t.Fatal(e.Trace(e.Forward(err)))
			}
			if !bytes.Equal(in, out) {
				t.Fatal("byte array not equal")
			}
			println("end", i)
		}(i)
	}
	wg.Wait()

	err = l.CloseClient()
	if err != nil {
		t.Fatal(e.Trace(e.Forward(err)))
	}

}