github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/flush_test.go (about)

     1  // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package pebble
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/petermattis/pebble/internal/datadriven"
    13  	"github.com/petermattis/pebble/vfs"
    14  )
    15  
    16  func TestManualFlush(t *testing.T) {
    17  	d, err := Open("", &Options{
    18  		FS: vfs.NewMem(),
    19  	})
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	datadriven.RunTest(t, "testdata/manual_flush", func(td *datadriven.TestData) string {
    25  		switch td.Cmd {
    26  		case "batch":
    27  			b := d.NewBatch()
    28  			if err := runBatchDefineCmd(td, b); err != nil {
    29  				return err.Error()
    30  			}
    31  			b.Commit(nil)
    32  			return ""
    33  
    34  		case "flush":
    35  			if err := d.Flush(); err != nil {
    36  				return err.Error()
    37  			}
    38  
    39  			d.mu.Lock()
    40  			s := d.mu.versions.currentVersion().DebugString()
    41  			d.mu.Unlock()
    42  			return s
    43  
    44  		case "async-flush":
    45  			d.mu.Lock()
    46  			cur := d.mu.versions.currentVersion()
    47  			d.mu.Unlock()
    48  
    49  			if _, err := d.AsyncFlush(); err != nil {
    50  				return err.Error()
    51  			}
    52  
    53  			err := try(100*time.Microsecond, 20*time.Second, func() error {
    54  				d.mu.Lock()
    55  				defer d.mu.Unlock()
    56  				if cur == d.mu.versions.currentVersion() {
    57  					return fmt.Errorf("flush has not occurred")
    58  				}
    59  				return nil
    60  			})
    61  			if err != nil {
    62  				return err.Error()
    63  			}
    64  
    65  			d.mu.Lock()
    66  			s := d.mu.versions.currentVersion().DebugString()
    67  			d.mu.Unlock()
    68  			return s
    69  
    70  		case "reset":
    71  			d, err = Open("", &Options{
    72  				FS: vfs.NewMem(),
    73  			})
    74  			if err != nil {
    75  				return err.Error()
    76  			}
    77  			return ""
    78  
    79  		default:
    80  			return fmt.Sprintf("unknown command: %s", td.Cmd)
    81  		}
    82  	})
    83  }