Merge branch 'master' into 4026-project-move-admin
[arvados.git] / services / crunchstat / crunchstat_test.go
index 00cd4d8365a7bd305715ebcf757dbe954085f77d..48988a136743d63a352a0eda0a38895a9e8a797c 100644 (file)
@@ -1,31 +1,42 @@
 package main
 
 import (
+       "os"
        "regexp"
        "testing"
 )
 
-func TestOpenAndReadAllFail(t *testing.T) {
-       log_chan := make(chan string)
+func TestReadAllOrWarnFail(t *testing.T) {
+       logChan = make(chan string)
        go func() {
-               defer close(log_chan)
-               if x, err := OpenAndReadAll("/nonexistent/file", log_chan); err == nil {
+               defer close(logChan)
+               // The special file /proc/self/mem can be opened for
+               // reading, but reading from byte 0 returns an error.
+               f, err := os.Open("/proc/self/mem")
+               if err != nil {
+                       t.Fatalf("Opening /proc/self/mem: %s", err)
+               }
+               if x, err := ReadAllOrWarn(f); err == nil {
                        t.Fatalf("Expected error, got %v", x)
                }
        }()
-       if _, ok := <-log_chan; !ok {
+       if _, ok := <-logChan; !ok {
                t.Fatalf("Expected error message about nonexistent file")
        }
-       if msg, ok := <-log_chan; ok {
+       if msg, ok := <-logChan; ok {
                t.Fatalf("Expected channel to close, got %s", msg)
        }
 }
 
-func TestOpenAndReadAllSuccess(t *testing.T) {
-       log_chan := make(chan string)
+func TestReadAllOrWarnSuccess(t *testing.T) {
+       logChan = make(chan string)
        go func() {
-               defer close(log_chan)
-               data, err := OpenAndReadAll("./crunchstat_test.go", log_chan)
+               defer close(logChan)
+               f, err := os.Open("./crunchstat_test.go")
+               if err != nil {
+                       t.Fatalf("Opening ./crunchstat_test.go: %s", err)
+               }
+               data, err := ReadAllOrWarn(f)
                if err != nil {
                        t.Fatalf("got error %s", err)
                }
@@ -33,7 +44,7 @@ func TestOpenAndReadAllSuccess(t *testing.T) {
                        t.Fatalf("data failed regexp: %s", err)
                }
        }()
-       if msg, ok := <-log_chan; ok {
+       if msg, ok := <-logChan; ok {
                t.Fatalf("Expected channel to close, got %s", msg)
        }
 }