vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vreplication/http_stream_mock.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     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  /*
    18   * To be used instead of HttpTest in cases where a test needs to do synchronized sequential reads are required from the
    19   * http stream. Note that this handles only one write at a time: the test should read the written data before the next
    20   * write. Data is sent to the channel only on a Flush.
    21   */
    22  
    23  package vreplication
    24  
    25  import (
    26  	"net/http"
    27  )
    28  
    29  // HTTPStreamWriterMock implements http.ResponseWriter and adds a channel to sync writes and reads
    30  type HTTPStreamWriterMock struct {
    31  	ch   chan any
    32  	data []byte
    33  }
    34  
    35  // NewHTTPStreamWriterMock returns a new HTTPStreamWriterMock
    36  func NewHTTPStreamWriterMock() *HTTPStreamWriterMock {
    37  	return &HTTPStreamWriterMock{ch: make(chan any, 1), data: make([]byte, 0)}
    38  }
    39  
    40  // Header is a stub
    41  func (w *HTTPStreamWriterMock) Header() http.Header {
    42  	return nil
    43  }
    44  
    45  // WriteHeader is a stub
    46  func (w *HTTPStreamWriterMock) WriteHeader(statuscode int) {
    47  }
    48  
    49  // Write buffers sent data
    50  func (w *HTTPStreamWriterMock) Write(data []byte) (int, error) {
    51  	w.data = append(w.data, data...)
    52  	return 0, nil
    53  }
    54  
    55  // Flush sends buffered data to the channel
    56  func (w *HTTPStreamWriterMock) Flush() {
    57  	w.ch <- w.data
    58  	w.data = w.data[:0]
    59  }