1// Copyright 2022 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package respipe 16 17import ( 18 "bytes" 19 "context" 20 "testing" 21 22 rdpb "src/tools/ak/res/proto/res_data_go_proto" 23 "google.golang.org/protobuf/proto" 24) 25 26func TestProduceConsume(t *testing.T) { 27 var b bytes.Buffer 28 29 ro := ResOutput{Out: &b} 30 resC := make(chan *rdpb.Resource) 31 ctx, cxlFn := context.WithCancel(context.Background()) 32 defer cxlFn() 33 34 errC := ro.Consume(ctx, resC) 35 ress := []*rdpb.Resource{ 36 { 37 Name: "hi", 38 }, 39 { 40 Name: "bye", 41 }, 42 { 43 Name: "foo", 44 }, 45 } 46 for _, r := range ress { 47 select { 48 case err := <-errC: 49 t.Fatalf("Unexpected err: %v", err) 50 case resC <- r: 51 } 52 } 53 close(resC) 54 if err := <-errC; err != nil { 55 t.Fatalf("unexpected err: %v", err) 56 } 57 ri := ResInput{In: &b} 58 59 resInC, errC := ri.Produce(ctx) 60 var got []*rdpb.Resource 61 for resInC != nil || errC != nil { 62 select { 63 case r, ok := <-resInC: 64 if !ok { 65 resInC = nil 66 continue 67 } 68 got = append(got, r) 69 case err, ok := <-errC: 70 if !ok { 71 errC = nil 72 continue 73 } 74 t.Fatalf("Unexpected err: %v", err) 75 } 76 } 77 if len(got) != len(ress) { 78 t.Fatalf("Got %d elements, expected %d", len(got), len(ress)) 79 } 80 for i := range ress { 81 if !proto.Equal(got[i], ress[i]) { 82 t.Errorf("Got: %+v wanted: %+v", got[i], ress[i]) 83 } 84 } 85} 86