steevehook
5/21/2019 - 7:40 AM

Go routine check for testing

func waitTillTableHasCount(conn sqlbuilder.Database, table string, n int) error {
	timeout := time.After(5 * time.Second)
	for {
		select {
		case <-timeout:
			count, err := conn.Collection(table).Find().Count()
			if err != nil {
				return err
			}
			return fmt.Errorf(
				"timeout occurred while waiting for table: %s to have expected: %d rows, but got: %d rows",
				table,
				n,
				int(count),
			)
		case <-time.After(10 * time.Millisecond):
			count, err := conn.Collection(table).Find().Count()
			if err == nil && int(count) == n {
				return nil
			}
		}
	}
}