github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sort_test.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql_test
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/sql/tests"
    18  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    19  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    20  )
    21  
    22  func TestOrderByRandom(t *testing.T) {
    23  	defer leaktest.AfterTest(t)()
    24  
    25  	params, _ := tests.CreateTestServerParams()
    26  	s, sqlDB, _ := serverutils.StartServer(t, params)
    27  	defer s.Stopper().Stop(context.Background())
    28  
    29  	seenOne := false
    30  	seenTwo := false
    31  	for {
    32  		row := sqlDB.QueryRow("SELECT * FROM (VALUES (1),(2)) ORDER BY random() LIMIT 1")
    33  		var val int
    34  		if err := row.Scan(&val); err != nil {
    35  			t.Fatal(err)
    36  		}
    37  		switch val {
    38  		case 1:
    39  			seenOne = true
    40  		case 2:
    41  			seenTwo = true
    42  		}
    43  		if seenOne && seenTwo {
    44  			break
    45  		}
    46  	}
    47  }