github.com/dolthub/go-mysql-server@v0.18.0/sql/session_test.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sql
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  type testNode struct{}
    26  
    27  func (*testNode) Resolved() bool {
    28  	panic("not implemented")
    29  }
    30  func (*testNode) WithChildren(...Node) (Node, error) {
    31  	panic("not implemented")
    32  }
    33  
    34  func (*testNode) Schema() Schema {
    35  	panic("not implemented")
    36  }
    37  
    38  func (*testNode) Children() []Node {
    39  	panic("not implemented")
    40  }
    41  
    42  func (*testNode) RowIter(ctx *Context) (RowIter, error) {
    43  	return newTestNodeIterator(), nil
    44  }
    45  
    46  type testNodeIterator struct {
    47  	Counter int
    48  }
    49  
    50  func newTestNodeIterator() RowIter {
    51  	return &testNodeIterator{
    52  		Counter: 0,
    53  	}
    54  }
    55  
    56  func (t *testNodeIterator) Next(ctx *Context) (Row, error) {
    57  	select {
    58  	case <-ctx.Done():
    59  		return nil, io.EOF
    60  
    61  	default:
    62  		t.Counter++
    63  		return NewRow(true), nil
    64  	}
    65  }
    66  
    67  func (t *testNodeIterator) Close(*Context) error {
    68  	panic("not implemented")
    69  }
    70  
    71  func TestSessionIterator(t *testing.T) {
    72  	require := require.New(t)
    73  	octx, cancelFunc := context.WithCancel(context.TODO())
    74  	defer cancelFunc()
    75  	ctx := NewContext(octx)
    76  
    77  	node := &testNode{}
    78  	iter, err := node.RowIter(ctx)
    79  	require.NoError(err)
    80  
    81  	counter := 0
    82  	for {
    83  		if counter > 5 {
    84  			cancelFunc()
    85  		}
    86  
    87  		_, err := iter.Next(ctx)
    88  
    89  		if counter > 5 {
    90  			require.Equal(io.EOF, err)
    91  			rowIter, ok := iter.(*testNodeIterator)
    92  			require.True(ok)
    93  
    94  			require.Equal(counter, rowIter.Counter)
    95  			break
    96  		}
    97  
    98  		counter++
    99  	}
   100  }