github.com/orofarne/hammy@v0.0.0-20130409105742-374fadfd6ecb/src/hammy/cmd_buffer_processor_test.go (about)

     1  package hammy
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  type SendBufferTestImpl struct {
     9  	RHandler RequestHandler
    10  }
    11  
    12  func (sb *SendBufferTestImpl) Push(data *IncomingData) {
    13  	sb.RHandler.Handle(*data)
    14  }
    15  
    16  func TestCmdBufferSendCommand(t *testing.T) {
    17  	rh := new(RequestHandlerTestImpl)
    18  	sb := SendBufferTestImpl{
    19  		RHandler: rh,
    20  	}
    21  
    22  	cbp := CmdBufferProcessorImpl{
    23  		SBuffer: &sb,
    24  	}
    25  
    26  	cmdb := make(CmdBuffer, 1)
    27  	cmdb[0].Cmd = "send"
    28  	cmdb[0].Options = make(map[string]interface{})
    29  	cmdb[0].Options["key"] = "key1"
    30  	cmdb[0].Options["value"] = "hello"
    31  	err := cbp.Process("host1", &cmdb)
    32  
    33  	if err != nil {
    34  		t.Errorf("Process error: %#v", err)
    35  	}
    36  
    37  	if host1, found := rh.Data["host1"]; !found {
    38  		t.Errorf("`host1` not found (data: %v)", rh.Data)
    39  	} else {
    40  		if key1, found := host1["key1"]; !found {
    41  			t.Errorf("`key1` not found (data: %v)", rh.Data)
    42  		} else {
    43  			if len(key1) != 1 {
    44  				t.Errorf("Expected len(key1) = 1, got: %d", len(key1))
    45  			} else {
    46  				if key1[0].Timestamp == 0 || key1[0].Timestamp > uint64(time.Now().Unix()) {
    47  					t.Errorf("Expected 0 <= timesetamp <= %v, got %#v", time.Now().Unix(), key1[0].Timestamp)
    48  				}
    49  				val, converted := key1[0].Value.(string)
    50  				if !converted { t.Errorf("Wrong type %T", key1[0].Value) } else {
    51  					if val != "hello" {
    52  						t.Errorf("Expected %#v, got %#v", "hello", val)
    53  					}
    54  				}
    55  			}
    56  		}
    57  	}
    58  
    59  	cmdb[0].Options["host"] = "host2"
    60  	cmdb[0].Options["value"] = "world"
    61  	err = cbp.Process("host1", &cmdb)
    62  
    63  	if err != nil {
    64  		t.Errorf("Process error: %#v", err)
    65  	}
    66  
    67  	time.Sleep(10 * time.Millisecond)
    68  
    69  	if host2, found := rh.Data["host2"]; !found {
    70  		t.Errorf("`host2` not found (data: %v)", rh.Data)
    71  	} else {
    72  		if key1, found := host2["key1"]; !found {
    73  			t.Errorf("`key1` not found (data: %v)", rh.Data)
    74  		} else {
    75  			if len(key1) != 1 {
    76  				t.Errorf("Expected len(key1) = 1, got: %d", len(key1))
    77  			} else {
    78  				if key1[0].Timestamp == 0 || key1[0].Timestamp > uint64(time.Now().Unix()) {
    79  					t.Errorf("Expected 0 <= timesetamp <= %v, got %#v", time.Now().Unix(), key1[0].Timestamp)
    80  				}
    81  				val, converted := key1[0].Value.(string)
    82  				if !converted { t.Errorf("Wrong type %T", key1[0].Value) } else {
    83  					if val != "world" {
    84  						t.Errorf("Expected %#v, got %#v", "world", val)
    85  					}
    86  				}
    87  			}
    88  		}
    89  	}
    90  }