github.com/omniscale/go-osm@v0.3.1/replication/internal/source/source_test.go (about)

     1  package source
     2  
     3  import (
     4  	"context"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	"testing"
    11  )
    12  
    13  func TestSeqPath(t *testing.T) {
    14  	if path := seqPath(0); path != "000/000/000" {
    15  		t.Fatal(path)
    16  	}
    17  	if path := seqPath(3069); path != "000/003/069" {
    18  		t.Fatal(path)
    19  	}
    20  	if path := seqPath(123456789); path != "123/456/789" {
    21  		t.Fatal(path)
    22  	}
    23  }
    24  
    25  func TestWaitTillPresent(t *testing.T) {
    26  	ctx := context.Background()
    27  	tmpdir, err := ioutil.TempDir("", "imposm_tests")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	defer os.RemoveAll(tmpdir)
    32  
    33  	exists := filepath.Join(tmpdir, "exists")
    34  	f, err := os.Create(exists)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	f.Close()
    39  	waitTillPresent(ctx, exists)
    40  
    41  	create := filepath.Join(tmpdir, "create")
    42  	go func() {
    43  		time.Sleep(200 * time.Millisecond)
    44  		f, err := os.Create(create)
    45  		if err != nil {
    46  			t.Fatal(err)
    47  		}
    48  		f.Close()
    49  	}()
    50  	waitTillPresent(ctx, create)
    51  
    52  	sub := filepath.Join(tmpdir, "sub", "dir", "create")
    53  	go func() {
    54  		time.Sleep(200 * time.Millisecond)
    55  		if err := os.Mkdir(filepath.Join(tmpdir, "sub"), 0755); err != nil {
    56  			t.Fatal(err)
    57  		}
    58  		time.Sleep(200 * time.Millisecond)
    59  		if err := os.Mkdir(filepath.Join(tmpdir, "sub", "dir"), 0755); err != nil {
    60  			t.Fatal(err)
    61  		}
    62  		time.Sleep(200 * time.Millisecond)
    63  		f, err := os.Create(sub)
    64  		if err != nil {
    65  			t.Fatal(err)
    66  		}
    67  		f.Close()
    68  	}()
    69  	waitTillPresent(ctx, sub)
    70  }
    71  
    72  func TestWaitTillPresent_Cancel(t *testing.T) {
    73  	ctx, cancel := context.WithCancel(context.Background())
    74  	tmpdir, err := ioutil.TempDir("", "imposm_tests")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	defer os.RemoveAll(tmpdir)
    79  
    80  	missing := filepath.Join(tmpdir, "missing")
    81  	go func() {
    82  		time.Sleep(50 * time.Millisecond)
    83  		cancel()
    84  	}()
    85  	if err := waitTillPresent(ctx, missing); err != nil {
    86  		t.Error("got err from canceled waitTillPresent", err)
    87  	}
    88  }