|
|
@@ -18,8 +18,12 @@ limitations under the License.
|
|
|
package memcache
|
|
|
|
|
|
import (
|
|
|
+ "exec"
|
|
|
+ "fmt"
|
|
|
"net"
|
|
|
+ "os"
|
|
|
"testing"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
const testServer = "localhost:11211"
|
|
|
@@ -35,12 +39,36 @@ func setup(t *testing.T) bool {
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
-func TestMemcache(t *testing.T) {
|
|
|
+func TestLocalhost(t *testing.T) {
|
|
|
if !setup(t) {
|
|
|
return
|
|
|
}
|
|
|
- c := New(testServer)
|
|
|
+ testWithClient(t, New(testServer))
|
|
|
+}
|
|
|
+
|
|
|
+// Run the memcached binary as a child process and connect to its unix socket.
|
|
|
+func TestUnixSocket(t *testing.T) {
|
|
|
+ sock := fmt.Sprintf("/tmp/test-gomemcache-%d.sock", os.Getpid())
|
|
|
+ cmd := exec.Command("memcached", "-s", sock)
|
|
|
+ if err := cmd.Start(); err != nil {
|
|
|
+ t.Logf("skipping test; couldn't find memcached")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer cmd.Wait()
|
|
|
+ defer cmd.Process.Kill()
|
|
|
+
|
|
|
+ // Wait a bit for the socket to appear.
|
|
|
+ for i := 0; i < 10; i++ {
|
|
|
+ if _, err := os.Stat(sock); err == nil {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ time.Sleep(25e6 * int64(i))
|
|
|
+ }
|
|
|
+
|
|
|
+ testWithClient(t, New(sock))
|
|
|
+}
|
|
|
|
|
|
+func testWithClient(t *testing.T, c *Client) {
|
|
|
// Set
|
|
|
foo := &Item{Key: "foo", Value: []byte("fooval"), Flags: 123}
|
|
|
if err := c.Set(foo); err != nil {
|