Ejemplo n.º 1
0
func TestUpdateMethods(t *testing.T) {
	sm := newTest()
	err := test.InserterBasic(sm, "Update")
	if err != nil {
		t.Fatalf(err.Error())
	}

	rndKeys := test.Data[rand.Intn(len(test.Data)-1)]
	newData := make(map[string]string, len(rndKeys))
	done := make(chan *test.Item, len(rndKeys))
	for k, _ := range rndKeys {
		tt := &test.Item{
			K:    k,
			V:    test.RandString(test.SizeItem),
			Done: done,
		}
		update(sm, tt)
	}
	for tt := range done {
		newData[tt.K] = tt.V
		if len(newData) >= len(rndKeys) {
			break
		}
	}
	for k, v := range newData {
		if val, found := sm.Find(k); !found || val.(string) != v {
			t.Fatalf("Cannot found key '%s'", k)
		}
	}
}
Ejemplo n.º 2
0
func TestUpdateMethods(t *testing.T) {
	sm := New()
	updater := func(args ...interface{}) error {
		m, ok := args[0].(map[string]string)
		if !ok {
			return fmt.Errorf("Get error type 'Map'")
		}
		stop := make(chan error, 1)
		c := make(chan test.Item, len(m))
		for k, v := range m {
			select {
			case err := <-stop:
				return err
			case c <- test.Item{K: k, V: v}:
			}
			sm.Update(k, func(value interface{}, found bool) interface{} {
				t := <-c
				if found {
					stop <- fmt.Errorf("Key '%s' is duplicated.", t.K)
				}
				return t.V
			})
		}
		return nil
	}
	err := test.DoPools(func(w *test.Worker) {
		for i := range test.Data {
			w.Add(updater, test.Data[i])
		}
	}, len(test.Data), "Update")
	if err != nil {
		t.Fatalf(err.Error())
	}
	if sm.Len() != test.CntWorks*test.CntItems {
		t.Fatal("Not equal.")
	}
	if err := test.FinderBasic(sm); err != nil {
		t.Fatalf(err.Error())
	}

	rndKeys := test.Data[rand.Intn(len(test.Data)-1)]
	newData := make(map[string]string, len(rndKeys))
	stop := make(chan error, 1)
	c := make(chan test.Item, len(rndKeys))
	for k, _ := range rndKeys {
		tt := test.Item{K: k, V: test.RandString(test.SizeItem)}
		select {
		case err := <-stop:
			t.Fatalf(err.Error())
		case c <- tt:
		}
		newData[k] = tt.V
		sm.Update(k, func(value interface{}, found bool) interface{} {
			t := <-c
			if !found {
				stop <- fmt.Errorf("Key '%s' not found.", t.K)
			}
			return t.V
		})
	}
	for k, v := range newData {
		if val, found := sm.Find(k); !found || val.(string) != v {
			t.Fatalf("Cannot found!")
		}
	}
}
Ejemplo n.º 3
0
func TestUpdateMethods(t *testing.T) {
	sm := New()
	wg := new(sync.WaitGroup)
	wg.Add(len(test.UniqMap))
	updater := func(args ...interface{}) error {
		m, ok := args[0].(map[string]string)
		if !ok {
			return fmt.Errorf("Get error type 'Map'")
		}
		stop := make(chan error, 1)
		defer close(stop)
		finger := func(key string, found bool) {
			if found {
				select {
				case stop <- fmt.Errorf("Key '%s' is duplicated.", key):
				default:
				}
			}
			wg.Done()
		}
		for k, v := range m {
			select {
			case err := <-stop:
				return err
			default:
				tt := &test.Item{
					K: k,
					V: v,
				}
				testUpdate(sm, tt, finger)
			}
		}
		return nil
	}
	err := test.DoPools(func(w *test.Worker) {
		for i := range test.Data {
			w.Add(updater, test.Data[i])
		}
	}, len(test.Data), "Update")
	if err != nil {
		t.Fatalf(err.Error())
	} else {
		wg.Wait()
	}
	if sm.Len() != test.CntWorks*test.CntItems {
		t.Fatal("Not equal.")
	}
	if err := test.FinderBasic(sm); err != nil {
		t.Fatalf(err.Error())
	}

	rndKeys := test.Data[rand.Intn(len(test.Data)-1)]
	newData := make(map[string]string, len(rndKeys))
	stop := make(chan error, 1)
	wg = new(sync.WaitGroup)
	wg.Add(len(rndKeys))
	finger := func(key string, found bool) {
		if !found {
			select {
			case stop <- fmt.Errorf("Key '%s' not found.", key):
			default:
			}
		}
		wg.Done()
	}
	for k, _ := range rndKeys {
		select {
		case err := <-stop:
			t.Fatalf(err.Error())
		default:
			tt := &test.Item{K: k, V: test.RandString(test.SizeItem)}
			newData[k] = tt.V
			testUpdate(sm, tt, finger)
		}
	}
	close(stop)
	wg.Wait()
	for k, v := range newData {
		if val, found := sm.Find(k); !found || val.(string) != v {
			t.Fatalf("Cannot found!")
		}
	}
}