forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for shard loader Fixes google#38. Change-Id: I5d97e10d0624018c439e0c65341ee4621f9118c8
- Loading branch information
Showing
2 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright 2018 Google Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package shards | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type loggingLoader struct { | ||
loads chan string | ||
drops chan string | ||
} | ||
|
||
func (l *loggingLoader) load(k string) { | ||
l.loads <- k | ||
} | ||
|
||
func (l *loggingLoader) drop(k string) { | ||
l.drops <- k | ||
} | ||
|
||
func advanceFS() { | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
|
||
func TestDirWatcherUnloadOnce(t *testing.T) { | ||
dir, err := ioutil.TempDir("", "") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
logger := &loggingLoader{ | ||
loads: make(chan string, 10), | ||
drops: make(chan string, 10), | ||
} | ||
_, err = NewDirectoryWatcher(dir, logger) | ||
if err == nil || !strings.Contains(err.Error(), "empty") { | ||
t.Fatalf("got %v, want 'empty'", err) | ||
} | ||
|
||
shard := filepath.Join(dir, "foo.zoekt") | ||
if err := ioutil.WriteFile(shard, []byte("hello"), 0644); err != nil { | ||
t.Fatalf("WriteFile: %v", err) | ||
} | ||
|
||
dw, err := NewDirectoryWatcher(dir, logger) | ||
if err != nil { | ||
t.Fatalf("NewDirectoryWatcher: %v", err) | ||
} | ||
defer dw.Close() | ||
|
||
if got := <-logger.loads; got != shard { | ||
t.Fatalf("got load event %v, want %v", got, shard) | ||
} | ||
|
||
// Must sleep because of FS timestamp resolution. | ||
advanceFS() | ||
if err := ioutil.WriteFile(shard, []byte("changed"), 0644); err != nil { | ||
t.Fatalf("WriteFile: %v", err) | ||
} | ||
|
||
if got := <-logger.loads; got != shard { | ||
t.Fatalf("got load event %v, want %v", got, shard) | ||
} | ||
|
||
advanceFS() | ||
if err := os.Remove(shard); err != nil { | ||
t.Fatalf("Remove: %v", err) | ||
} | ||
|
||
if got := <-logger.drops; got != shard { | ||
t.Fatalf("got drops event %v, want %v", got, shard) | ||
} | ||
|
||
advanceFS() | ||
if err := ioutil.WriteFile(shard+".bla", []byte("changed"), 0644); err != nil { | ||
t.Fatalf("WriteFile: %v", err) | ||
} | ||
|
||
dw.Close() | ||
|
||
select { | ||
case k := <-logger.loads: | ||
t.Errorf("spurious load of %q", k) | ||
case k := <-logger.drops: | ||
t.Errorf("spurious drops of %q", k) | ||
default: | ||
} | ||
} |