github.com/enmand/kubernetes@v1.2.0-alpha.0/pkg/watch/json/decoder_test.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package json
    18  
    19  import (
    20  	"encoding/json"
    21  	"io"
    22  	"testing"
    23  	"time"
    24  
    25  	"k8s.io/kubernetes/pkg/api"
    26  	"k8s.io/kubernetes/pkg/api/testapi"
    27  	"k8s.io/kubernetes/pkg/runtime"
    28  	"k8s.io/kubernetes/pkg/util"
    29  	"k8s.io/kubernetes/pkg/watch"
    30  )
    31  
    32  func TestDecoder(t *testing.T) {
    33  	table := []watch.EventType{watch.Added, watch.Deleted, watch.Modified, watch.Error}
    34  
    35  	for _, eventType := range table {
    36  		out, in := io.Pipe()
    37  		decoder := NewDecoder(out, testapi.Default.Codec())
    38  
    39  		expect := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
    40  		encoder := json.NewEncoder(in)
    41  		go func() {
    42  			data, err := testapi.Default.Codec().Encode(expect)
    43  			if err != nil {
    44  				t.Fatalf("Unexpected error %v", err)
    45  			}
    46  			if err := encoder.Encode(&WatchEvent{eventType, runtime.RawExtension{RawJSON: json.RawMessage(data)}}); err != nil {
    47  				t.Errorf("Unexpected error %v", err)
    48  			}
    49  			in.Close()
    50  		}()
    51  
    52  		done := make(chan struct{})
    53  		go func() {
    54  			action, got, err := decoder.Decode()
    55  			if err != nil {
    56  				t.Fatalf("Unexpected error %v", err)
    57  			}
    58  			if e, a := eventType, action; e != a {
    59  				t.Errorf("Expected %v, got %v", e, a)
    60  			}
    61  			if e, a := expect, got; !api.Semantic.DeepDerivative(e, a) {
    62  				t.Errorf("Expected %v, got %v", e, a)
    63  			}
    64  			t.Logf("Exited read")
    65  			close(done)
    66  		}()
    67  		<-done
    68  
    69  		done = make(chan struct{})
    70  		go func() {
    71  			_, _, err := decoder.Decode()
    72  			if err == nil {
    73  				t.Errorf("Unexpected nil error")
    74  			}
    75  			close(done)
    76  		}()
    77  		<-done
    78  
    79  		decoder.Close()
    80  	}
    81  }
    82  
    83  func TestDecoder_SourceClose(t *testing.T) {
    84  	out, in := io.Pipe()
    85  	decoder := NewDecoder(out, testapi.Default.Codec())
    86  
    87  	done := make(chan struct{})
    88  
    89  	go func() {
    90  		_, _, err := decoder.Decode()
    91  		if err == nil {
    92  			t.Errorf("Unexpected nil error")
    93  		}
    94  		close(done)
    95  	}()
    96  
    97  	in.Close()
    98  
    99  	select {
   100  	case <-done:
   101  		break
   102  	case <-time.After(util.ForeverTestTimeout):
   103  		t.Error("Timeout")
   104  	}
   105  }