If a witness refuses to testify, or to be sworn, or to give a deposition, he shall be imprisoned so long as he refuses, or until he testifies before an officer who is authorized to take his testimony. The final disposition of the case in which he so refuses shall discharge him from imprisonment.